Jump to content
  • Advertisement
Sign in to follow this  
Gubber

Help

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

im getting an error "MObject: no appropriate default constructor avaliable", i found a similar error on the fourms here and you were able to help him but I still cant figure it out. Whats wrong with this? /*animal.h*/ #ifndef ANIMAL_H #define ANIMAL_H #include "Object.h" #include "ATask.h" struct Animal : MObject { ATask* mtask; Animal(string name, int x, int y, char rep, int col, bool obs); }; #endif /*animal.cpp*/ #include "Animal.h" Animal::Animal(string name, int x, int y, char rep, int col, bool obs) { Animal::name = name; Animal::x = x; Animal::y = y; Animal::col = col; Animal::rep = rep; Animal::obstruction = obs; } the decleration is currently in Main.h and is as follows Animal *mob = new Animal("Ian", 10, 5, (char)CLOSEDFACE, 12, true); Thanks, Gubber

Share this post


Link to post
Share on other sites
Advertisement
Like is says, you dont have a default constructor for MObject. And by default constructor it means one with no parameters. I assume you have a constructor on MObject that takes some parameter (the same list as the Animal constructor probably). You need to explictly specify which constructor Animal should use when constructing its parent.

Seeing as Animal has no member variables I also assume those are inherited from MObject. What you probably are trying to get to is this (leaving off some variables for simplicity):


class MObject
{
public:
MObject(string name, int x, int y);
private:
string name_;
int x_;
int y_;
};

MObject::MObject(string name, int x, int y) : name_(name), x_(x), y_(y)
{
}

class Animal : public MObject
{
public:
Animal(string name, int x, int y);
};

Animal::Animal(string name, int x, int y) : MObject(name, x, y)
{
}

Share this post


Link to post
Share on other sites
thanks, that works perfectley, but I dont understand why it works.

whats the advantage of having : Function call, instead of calling the function inside the class. I also noticed the Mobject that you drew out had some : stuff before the variables. I assume that you were setting them to the passed in values. Is _name the same as MObject::name? what else can I do with this : thing? is it limited to constructors? Link to tutorial/name of something i could google?

Thank you for your help,
Gubber

Share this post


Link to post
Share on other sites
Its not a function call, its an initializer list, you can read about it here.

And the trailing underscores are just my way of saying that something is a member variable. They have no inherent meaning to the langauge.

Alan

Share this post


Link to post
Share on other sites
Quote:
Original post by Gubber
what else can I do with this : thing? is it limited to constructors? Link to tutorial/name of something i could google?


the :: and : are basic syntax of C++ relating to classes/structs as are the keywords public, protected and private. Read this:

http://www.cplusplus.com/doc/tutorial/

particularly the chapters titled "Classes(I)" and "Classes(II)"

-me

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!