Class / array question

Started by
9 comments, last by DividedByZero 8 years, 6 months ago
Hi Guys,

Not sure if this can be done the way I would like, but I'll put the question out there.

I have an array that contains three floats in each location, which I am using as vertex data.

I have a public variable verticies[] inside a class. Is there a way I can assign variable length data to this variable without the compiler throwing an error?

This is the class (pretty basic)


class Object
{
public:
	Object(){}
	~Object(){}

	VertexPos verticies[];
private:
};
And I'd like to fill the verticies[] like this;


mObject->verticies[] = 
{
	DirectX::XMFLOAT3((float)-0.5, (float)-0.5, (float)0), DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0),
	DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0),
};
Can this be done? Or is this going to be a problem as there is no static size defined in the class?

Thanks in advance smile.png
Advertisement
I have done this to make the class ready for the amount of verts


	void MakeSpace(int verts)
	{
		verticies = new VertexPos[verts];
	}

	VertexPos* verticies;

How do I set the values? I doesn't seem to be as simple as


mObject->verticies[] = 
{
	DirectX::XMFLOAT3((float)-0.5, (float)-0.5, (float)0), DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0),
	DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0),
};

Thanks again smile.png

you can use a std::vector.

If your compiler supports c++11 you can even use the array initializer syntax like in your example.


#include <vector>

class Object
{
public:
	Object(){}
	~Object(){}

	std::vector<VertexPos> vertices;
private:
};

mObject->vertices = { ... }
Thanks for the tip. Although I just tried that and the compiler complains of 'C2679 binary '=': no operator found which takes a right-hand operand of type 'initializer list' (or there is no acceptable conversion)'.

I am using VS.net 2015.

Weird, I just tried with VS.net 2015 community edition and it works. Can you post the code of the VertexPos class and the way you initialize the list?

Yep, now worries.

This is the class as at now


#pragma once
#include <DirectXMath.h>
#include <vector>

// Structures
struct VertexPos
{
	DirectX::XMFLOAT3 pos;
};

class Object
{
public:
	Object() {}
	~Object() {}

	float x;
	float y;
	float z;
	int nVerts;
	std::vector<VertexPos> vertices;
private:
};
And the initialisation code


Object* mObject = new Object();
mObject->vertices =
{
	DirectX::XMFLOAT3((float)-0.5, (float)-0.5, (float)0), DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0),
	DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)0.5, (float)0), DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0),
};
Thanks heaps for you help smile.png



It seems happy enough if I do this


	mObject->vertices[0] = { DirectX::XMFLOAT3((float)-0.5, (float)-0.5, (float)0) };
	mObject->vertices[1] = { DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0) };
	mObject->vertices[2] = { DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0) };
	mObject->vertices[3] = { DirectX::XMFLOAT3((float)-0.5, (float)0.5, (float)0) };
	mObject->vertices[4] = { DirectX::XMFLOAT3((float)0.5, (float)0.5, (float)0) };
	mObject->vertices[5] = { DirectX::XMFLOAT3((float)0.5, (float)-0.5, (float)0) };
This is far from ideal, as you can imagine smile.png

But the 'one line' style initialisation just keeps failing for some reason sad.png

Ah!

You are trying to initialize a vector of VertexPos with a list of XMFLOAT3's which is not going to work.

So either you need to make it std::vector<XMFLOAT3> or put another pair of parentheses around the initializer elements like so:


mObject->vertices =
{
	{XMFLOAT3(0,0,0)}, {XMFLOAT3(0,0,0)}, {XMFLOAT3(0,0,0)}
};
Nice! That looks like we are back on track!

Thank you so much for your help smile.png

Up-voted all of your suggestions, as it is the least I can do in return smile.png
Just another quick question.

To read the data back, you'd now have to iterate through it like a normal vector, right? (As it doesn't seem to behave like a normal array now).

This topic is closed to new replies.

Advertisement