C++ - Errors / Warnings - After compiling

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

1. Main.cpp:(.text+0x1f): undefined reference to `Parent::Parent()'

A. The problem

A very common error!
Did you include the parent.cpp file in your Makefile?
Did you compile with this parent.cpp?

B. The solution

Add the correct name of the file into the Makefile to compile it with others .cpp files.

SRC= main.cpp parent.cpp

2. Expected class-name before ‘,’ token
/ expected class-name before ‘{’ token

A. The problem

You forgot to include the two headers of the two parent classes needed.

class Child: public Mother, public Father {
public:
    Child();
    ~Child();
};

B. The solution

Include the right files:

#include "father.hh"
#include "mother.hh"

class Child: public Mother, public Father {
public:
    Child();
    ~Child();
};

3. Virtual base ‘Parent’ inaccessible in ‘Child’ due to ambiguity

A. The problem

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();
};

B. The solution

Add the virual keyword before the public parent call.

// father.hh

#include "parent.hh"

class Father: virtual public Parent {
public:
    Father();
    virtual ~Father();
};

4. ‘Parent’ is an ambiguous base of ‘Child’

A. The problem

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();
};

B. The solution

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();
};

 

5.  Constructor.cpp:(.text+0x14): undefined reference to `vtable for Constructor'

A. The problem

This problem may be different things.

For example it may missing something. The Constructor is there but the Destructor isn't.

B. The solution

Implementing the Destructor.

 

6. error: variable ‘std::ifstream ifile’ has initializer but incomplete type

A. The problem

You forgot to include <fstream>. 

B. The solution

Include it:

#include <fstream>

 

7. error: expected primary-expression before ‘<<’ token

A. The problem

You certainly use a semicolon before the end of the function. 

Exemple:

std::cout << typeInt->getType(); << std::endl;

B. The solution

Remove it:

std::cout << typeInt->getType() << std::endl;
 

 

 

8. error: lvalue required as left operand of assignment

A. The problem

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;

B. The solution

Change the place if the asterisk before the right parenthesis.

 

*(i - 1) = 8;

 

9. error: looser throw specifier for ‘virtual MyClass::~MyClass()’
error:   overriding ‘virtual MyException::~MyException() throw ()’

 

A. The problem

In the derived class from std::exception, we have the following destructor:

virtual ~MyException() throw();

 

B. The solution

Add the same prototype destructor to your derived class from MyException.

 

virtual ~MyClass() throw();