• Advertisement
Sign in to follow this  

Polygon struct and new operator

This topic is 4396 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

i have a struct that reads
struct Polygon
{
    int nidx;
    int* index;
};
but when I try to create an array like this:
Polygon m_pol = new Polygon;
I get an error saying there's an error before the [. Is it something to do with the pointer in the struct/ do I need a class and constructor? What actually is the problem? regard... JPulham

Share this post


Link to post
Share on other sites
Advertisement
struct polygon_t
{
int nidx;
int* index;
};

polygon_t *m_pol = new polygon_t;

And it works.

Share this post


Link to post
Share on other sites
soz...I posted my cod ewrong...
ihave a calss with
Polygon* m_pol;

in it and a loader function that contains

m_pol = new Polygon[m_npol];

but I get the error:

syntax error before '[' token

I am a complete noob at debuging...so plz help

Share this post


Link to post
Share on other sites
Quote:
Original post by JPulham
soz...I posted my cod ewrong...
ihave a calss with
Polygon* m_pol;

in it and a loader function that contains

m_pol = new Polygon[m_npol];

but I get the error:

syntax error before '[' token

I am a complete noob at debuging...so plz help


This kind of error (syntax error before token 'something') is not rare, and most of the time the compiler screms because the token which is right before 'something' is not understood - ie it is probably not declared. In your case, you have to check if you correctly included your Polygon declaration and if the Polygon struct is correctly declared.

BTW, this is a bug, this is a compilation error. Don't be affraid of them: understanding compilation errors is a matter of experience.

Regards,

Share this post


Link to post
Share on other sites
0) For Beginners is there for a reason.

1) In C++, don't maintain a dynamically allocated array and length count like that. Use std::vector instead:


#include <vector>

struct Polygon {
std::vector<int> indices;
};

vector<Polygon> myPolygons(n_pol);
// initial number of polygons - the container can be resized, too


If you explicitly *want* to *not* resize the containers, and would like the compiler to make sure you don't try, you may also be interested in Boost::array.

2) Yeah, looks like something is messed up with your declarations. If you have a separate header for declaring the Polygon struct, you will want to read this.

Share this post


Link to post
Share on other sites
OK... now I feel noobish...
I know about C++, I've been doing it for years...
(My GCSE's get in the way of learning though :P )
ever heard of a Ploygon?
its what typo i put in for every instance except for that one...
I must of typed Plygon 5 or 6 times!
man my typing was off.
As for STD::vector... Why not?
works on linux and I'm using STD::list for my SceneGraph.
but thnx for the talk any way... let me run things through my head.
Now my game engine has Mesh support (only 3DS and user mesh), OpenGL and DirectX!!!

Share this post


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

  • Advertisement