As the same for the C - Error / Warning section, I made this one to summarize common mistakes when I tried to compile my code.
So let's go to see some good errors and warnings in C++ (I'm sure it is also a great moment for you when you discovered these errors).
A very common error!
Did you include the parent.cpp file in your Makefile?
Did you compile with this parent.cpp?
Add the correct name of the file into the Makefile to compile it with others .cpp files.
SRC= main.cpp parent.cpp
You forgot to include the two headers of the two parent classes needed.
class Child: public Mother, public Father { public: Child(); ~Child(); };
Include the right files:
#include "father.hh" #include "mother.hh" class Child: public Mother, public Father { public: Child(); ~Child(); };
You have a Child class that inherits from a Mother and a Father class.
These two parents inherit from the Parent class.
But only one of them inherits with the virtual keyword.
// parent.hh class Parent { public: Parent(); virtual ~Parent(); };
// mother.hh #include "parent.hh" class Mother: virtual public Parent { public: Mother(); virtual ~Mother(); };
// father.hh #include "parent.hh" class Father: public Parent { public: Father(); virtual ~Father(); };
//child.hh #include "father.hh" #include "mother.hh" class Child: public Mother, public Father { public: Child(); virtual ~Child(); };
Add the virual keyword before the public parent call.
// father.hh #include "parent.hh" class Father: virtual public Parent { public: Father(); virtual ~Father(); };
You have a Child class that inherits from a Mother and a Father class.
These two parents inherit from the Parent class.
But neither the Mother nor the Father inherits from the Parent with the virtual keyword.
This is the diamond problem because we have a Child that inherits from two classes (Mother and Father) that both inherit from a Parent.
The compiler doesn't know which class called because without the virtual keyword, it creates the class Parent twice.
And we have then two inheritance trees.
// parent.hh class Parent { public: Parent(); virtual ~Parent(); };
// mother.hh #include "parent.hh" class Mother: public Parent { public: Mother(); virtual ~Mother(); };
// father.hh #include "parent.hh" class Father: public Parent { public: Father(); virtual ~Father(); };
//child.hh #include "father.hh" #include "mother.hh" class Child: public Mother, public Father { public: Child(); virtual ~Child(); };
Add the virual keyword before the public parent call.
The compiler will now creates only one instance of the Parent class, and links the both children (Father and Mother) to the same memory area of the unique Parent.
// father.hh #include "parent.hh" class Father: virtual public Parent { public: Father(); virtual ~Father(); };
// mother.hh #include "parent.hh" class Mother: virtual public Parent { public: Mother(); virtual ~Mother(); };
This problem may be different things.
For example it may missing something. The Constructor is there but the Destructor isn't.
Implementing the Destructor.
You forgot to include <fstream>.
Include it:
#include <fstream>
You certainly use a semicolon before the end of the function.
Exemple:
std::cout << typeInt->getType(); << std::endl;
Remove it:
std::cout << typeInt->getType() << std::endl;
The asterisk (*) is not put at the right place.
Exemple:
You are using an iterator i and you want to put the 8 value inside the value before the current iterator.
So you try to do:
(*i - 1) = 8;
Change the place if the asterisk before the right parenthesis.
*(i - 1) = 8;
In the derived class from std::exception, we have the following destructor:
virtual ~MyException() throw();
Add the same prototype destructor to your derived class from MyException.
virtual ~MyClass() throw();