Skip to main content
GameDev.net gamedev.net
🔒 Locked

Polygon struct and new operator

Started by JPulham Apr 7, 2006 at 5:03 AM 5 replies 650+ views
Original Post
JPulham
JPulham
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
sph
sph
struct polygon_t
{
int nidx;
int* index;
};

polygon_t *m_pol = new polygon_t;

And it works.
JPulham
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
smart_idiot
smart_idiot
Edit Nevermind, I can't read.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Emmanuel Deloget
Emmanuel Deloget
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,
Zahlman
Zahlman
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.
JPulham
JPulham
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!!!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.