Jump to content
  • Advertisement
Sign in to follow this  
godsenddeath

problem compiling something

This topic is 3878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for some reason, this part of my code won't compile,

	primitive = new Vertex[4];
	
	primitive = 
	{
		{-5.0f,5.0f,0.0f,D3DCOLOR_XRGB(255,255,255)},
		{5.0f,5.0f,0.0f,D3DCOLOR_XRGB(255,255,255)},
		{-5.0f,-5.0f,0.0f,D3DCOLOR_XRGB(255,255,255)},
		{5.0f,-5.0f,0.0f,D3DCOLOR_XRGB(255,255,255)}
	};



these are what visual studio are popping out 1>.\square.cpp(18) : error C2059: syntax error : '{' 1>.\square.cpp(18) : error C2143: syntax error : missing ';' before '{' 1>.\square.cpp(19) : error C2059: syntax error : '}' 1>.\square.cpp(19) : error C2143: syntax error : missing ';' before ',' 1>.\square.cpp(20) : error C2143: syntax error : missing ';' before '{' 1>.\square.cpp(20) : error C2059: syntax error : '}' 1>.\square.cpp(20) : error C2143: syntax error : missing ';' before ',' 1>.\square.cpp(21) : error C2143: syntax error : missing ';' before '{' 1>.\square.cpp(21) : error C2059: syntax error : '}' 1>.\square.cpp(21) : error C2143: syntax error : missing ';' before ',' 1>.\square.cpp(22) : error C2143: syntax error : missing ';' before '{' 1>.\square.cpp(22) : error C2059: syntax error : '}' assigning values to an array like this isn't something i'm used to so i'm wondering if i'm just coding something wrong, does anyone have a suggestion? thanks

Share this post


Link to post
Share on other sites
Advertisement
The correct usage for the array or structure initialization syntax is like so:

struct vertex {
float x, y, z, w;
};

int main( int argc, char* argv[] ) {
vertex primitive[4] = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}};

return 0;
}



I don't believe it can be used in conjunction with the new keyword, and cannot be used to assign to a variable after initialization.

Share this post


Link to post
Share on other sites
the problem is, this code is within a class, so i can't initialize it when it's declared. will i have to do it liek this?

primitive[0].x = 1;
primitive[0].y = 1;

etc....?

Share this post


Link to post
Share on other sites
Provide the Vertex class with an appropriate constructor that takes the four members as arguments. Then, using the copy constructor, you can do something like this:


primitive = new Vertex[4];
primitive[0] = Vertex(1,2,3,4);
primitive[1] = Vertex(5,6,7,8);
// etc



That's the best I can come up with, but my C++ is slightly rusty.

Share this post


Link to post
Share on other sites
Why would you do your own memory management here?


struct Vertex {
float x, y, z;
D3DCOLOR c; // or whatever it's called
Vertex(float x, float y, float z, D3DCOLOR c): x(x), y(y), z(z), c(c) {}
};

std::vector<Vertex> primitives;

primitives.push_back(Vertex(-5.0f, 5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));
primitives.push_back(Vertex( 5.0f, 5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));
primitives.push_back(Vertex(-5.0f,-5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));
primitives.push_back(Vertex( 5.0f,-5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Why would you do your own memory management here?


struct Vertex {
float x, y, z;
D3DCOLOR c; // or whatever it's called
Vertex(float x, float y, float z, D3DCOLOR c): x(x), y(y), z(z), c(c) {}
};

std::vector<Vertex> primitives;

primitives.push_back(Vertex(-5.0f, 5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));
primitives.push_back(Vertex( 5.0f, 5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));
primitives.push_back(Vertex(-5.0f,-5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));
primitives.push_back(Vertex( 5.0f,-5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)));
@The OP: Also, if you want something vaguely similar to bracketed initialization syntax, you can use boost::assign::list_of.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!