Sign in to follow this  
lonewolff

Class / array question

Recommended Posts

lonewolff    1183
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

Share this post


Link to post
Share on other sites
lonewolff    1183
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

Share this post


Link to post
Share on other sites
Madhed    4095

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 = { ... }

Share this post


Link to post
Share on other sites
lonewolff    1183
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.

Share this post


Link to post
Share on other sites
Madhed    4095

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?

Edited by Madhed

Share this post


Link to post
Share on other sites
lonewolff    1183
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 Edited by DarkRonin

Share this post


Link to post
Share on other sites
Madhed    4095

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)}
};

Share this post


Link to post
Share on other sites
Madhed    4095

You can iterate a std::vector like a normal array using the subscript operator (square brackets)

mObject->vertices[0] // returns the first vertex

Vector is a weird name for that container, especially if you are coming from a math/graphics background.

std::vector is just a dynamic array that you can resize and it manages the memory for you.

You can call vertices.size() to get the number of vertices, vertices.push_back(vertex) to append a new vertex and more cool stuff.

 

I'd suggest you go through a beginner's tutorial for the stl container classes as they are a fundamental building block of modern c++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this