oak.cpp: In constructor ‘oak::oak()’:
oak.cpp:14: error: class ‘oak’ does not have any field named ‘type’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘probCatch’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘status’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘wetness’
oak.cpp:14: error: class ‘oak’ does not have any field named ‘burnTime’
oak.cpp: In constructor ‘oak::oak(std::string, double, int, double, int)’:
oak.cpp:23: error: class ‘oak’ does not have any field named ‘type’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘probCatch’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘status’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘wetness’
oak.cpp:23: error: class ‘oak’ does not have any field named ‘burnTime’
pine.cpp: In constructor ‘pine::pine()’:
pine.cpp:13: error: class ‘pine’ does not have any field named ‘type’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘probCatch’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘status’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘wetness’
pine.cpp:13: error: class ‘pine’ does not have any field named ‘burnTime’
pine.cpp: In constructor ‘pine::pine(std::string, double, int, double, int)’:
pine.cpp:22: error: class ‘pine’ does not have any field named ‘type’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘probCatch’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘status’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘wetness’
pine.cpp:22: error: class ‘pine’ does not have any field named ‘burnTime’

This is my first time doing polymorhpism and I'm not sure how to do it. I'm making some mistakes that are messing up the program and I can't catch them. Please help me if you can.

05-07-2008

forseti42

Ok, according to the instructions, I have to create a base class pointer named treePtr that points to a tree object. Then that in turn will assign the address of each object to treePtr and call showSymbol() using:

Code:

(*treePtr).showSymbol(cout);

or

Code:

treePtr->showSymbol(cout);

I haven't done pointers before either. Can someone help me with creating one or give me links to sites that may help.

Thanks a bunch!
Ryan

05-07-2008

medievalelks

What book are you using? I didn't read very much of the post, just the title and the fact that your base class doesn't have any virtual functions.

The problem is not with an attempt to use polymorphism per se. The problem is that type, probCatch, status, wetness and burnTime are member variables from the base class (tree), so they cannot be initialised in the derived class (oak). You can assign to them in the body of the constructor since they are declared protected, but it would be more efficient to invoke the base class constructor from the initialisation list of the derived class constructor:

This would still work even if those member variables were private (and I find it better to make them private).

However, you do have an issue where polymorphism is concerned: namely, your base class has no virtual member functions (in particular, its destructor is not virtual), thus tree is not a polymorphic base class to begin with.

05-07-2008

forseti42

Thank you laserlight! That definitely helps.

That would take care of the default constructor for the classes, but how would I go about adding a constructor with parameters in the oak and pine class?

The whole project I'm working on right now uses both polymorphism and inheritance. Right now I'm just working with the tree and derived tree classes. After this I have to add them to my forest class and create a forest destructor.

05-07-2008

laserlight

Quote:

That would take care of the default constructor for the classes, but how would I go about adding a constructor with parameters in the oak and pine class?

The same way, of course. You just use the arguments instead.

Quote:

The whole project I'm working on right now uses both polymorphism and inheritance. Right now I'm just working with the tree and derived tree classes.

We're saying that you are using inheritance without the possibility of polymorphism. If your tree class is to be a proper polymorphic base class, it must have at least one virtual member function, and its destructor should be virtual.

05-07-2008

forseti42

Ok, I needed to call the original tree constructor with parameters. It works now.

I should have reworded my last statement. Right now I'm only working on the Inheritance part, but when apply these new classes in the forest class, I will have to use polymorphism.

However, I still don't know how to create a base class pointer, which I mentioned in an earlier post.

Thanks a bunch!
Ryan

05-07-2008

laserlight

Quote:

I should have reworded my last statement. Right now I'm only working on the Inheritance part, but when apply these new classes in the forest class, I will have to use polymorphism.

Before you even involve the forest class, you should already decide on what member functions should be virtual. In other words, you need to ask yourself: what kind of behaviour do all trees have? Of these kinds of behaviour, what should be fixed for all trees (i.e., they should be non-virtual member functions), and what should be allowed to vary for specific types of trees (i.e., they should be virtual member functions)?

Quote:

However, I still don't know how to create a base class pointer, which I mentioned in an earlier post.

The base class is named tree. A pointer to an object of type T is itself of type T*. Therefore, you would create a base class pointer with:

Code:

tree* tree_ptr;

05-07-2008

forseti42

I created the tree pointer like so:

Code:

tree *treePtr;

treePtr=&a;

(*treePtr).showSymbol(cout);

b.showSymbol(cout);

c.showSymbol(cout);

However, I now have to go to my forest class and change the grid[21][21] to a grid of tree pointers. I don't feel that my tree pointer is right for what I need to do.

05-07-2008

laserlight

Quote:

However, I now have to go to my forest class and change the grid[21][21] to a grid of tree pointers. I don't feel that my tree pointer is right for what I need to do.

That's the correct thing to do. In C++, polymorphism works through pointers and references.

Incidentally,

Code:

(*treePtr).showSymbol(cout);

may be replaced with this syntactic sugar:

Code:

treePtr->showSymbol(cout);

05-07-2008

hk_mp5kpdw

Your header files have using namespace std; in them which should be avoided (explicitly qualify all std objects with std::).

The headers also lack a #include <string> statement for the string objects declared within. Right now, your code (may) compile because you just so happen to #include <string> in your source files before you happen to include the relevant header but if for some reason you switch around the order of the #include statements and put #include <tree.h> before #include <string> for example you might find yourself scratching your head at why things suddenly don't compile like they used to.

05-07-2008

forseti42

This is an intro c++ course and they explicitly tell us to use "using namespace std".

05-07-2008

forseti42

Ok, everything is going well, but I'm having trouble with my destructor for my forest class: