Sign in to follow this  
Gubber

Help

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this