Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Allebarde

Structures, Class, constructor :o)

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

Hi I have a big problem with my structure: struct METEORS { POINT3D Pos; VECTOR3D Dir; float RotX,RotY,RotZ; _PARTICLE part(50); }; Here for each meteor, i want to create 50 particles: so my particle class has a constructor receiving as an argument, the nbr of particles to create. If I use this code i have this error: error C2059: syntax error : ''constant'' If I use _PARTICLE part; i have this error error C2512: METEORS'' : no appropriate default constructor available And if I do: _PARTICLE part; METEORS() { _PARTICLE part(50); } error C2512: ''_PARTICLE'' : no appropriate default constructor available What shall I do???? I hope that someone can help me Bye!

Share this post


Link to post
Share on other sites
Advertisement
Use square brackets [ ] to define an array. The use of parenthesis ( ) when creating objects in C++ means you want to pass that as an argument to a constructor.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The correct way to do it is this:

METEORS()art(50)
{
}

Share this post


Link to post
Share on other sites
struct METEORS : _PARTICLE
{
POINT3D Pos;
VECTOR3D Dir;
float RotX,RotY,RotZ;




METEORS() : _PARTICLE part(50) {}

};

Share this post


Link to post
Share on other sites
When you define a structure, you have to know how big each piece is going to be at compile-time. You can't have METEORS take a constructor with a variable and expect it to create that number of entries. What you need to look at doing is dynamically allocating that many elements and assigning it to a pointer inside your function. I prefer using vector as a replacement for dynamic arrays, but I'm guessing you aren't familiar with STL.

This code, I believe, will do what you want:

    
struct Particle
{
// particle definition

};

class Meteor
{
public:
Meteor (int nParticles);
~Meteor ();
private:
Particle *m_particleArray;
};

Meteor::Meteor (int nParticles)
: m_particleArray (new Particle[nParticles])
{
}

Meteor::~Meteor ()
{
delete [] m_particleArray;
}

int main ()
{
Meteor m(50);
return 0;
}




[edited by - Stoffel on June 1, 2002 5:00:45 PM]

Share this post


Link to post
Share on other sites
Yeah It seem It will work, but now if I want to create 10 meteors, with 50 particle for each one:

extern METEORS _meteors[10]; //in the header file

METEORS _meteors[0](50); //it doesn''t work

Can I do this:

METEORS _meteors[10](50,50,50,50,50..); //??

Thanks

Share this post


Link to post
Share on other sites
You should really pick up a book on C++ programming. You cannot create a dynamic array on the heap and initialize each individual element in one step.

In regards to your first problem:


struct METEORS
{
POINT3D Pos;
VECTOR3D Dir;
float RotX,RotY,RotZ;

_PARTICLE part(50);

};


I''m assuming _PARTICLE is another struct or class. You are trying to initialize a single instance of _PARTICLE with a value of 50. The _PARTICLE class/struct doesn''t appear to have a constructor defined so the compiler complains.

You want:


struct METEORS
{
POINT3D Pos;
VECTOR3D Dir;
float RotX,RotY,RotZ;

_PARTICLE part[50];

};


Stoffel''s code is what you should be using. Go grab a book on C++ or do a search on Google.

Dire Wolf
www.digitalfiends.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!