C ++ multiple definition error for every constructor, method and operator overloading

I realize there are many "multiple definition" questions here, but I've spent the last 2 hours looking for explanations and haven't found any. Sorry if this is a duplicate.

Now I have 2 classes: Array.h and Vector.h. There are also no global variables, and none of them depend on the other (i.e. Array does not use Vector and Vector, does not use Array). The implementations are in the .h files.

Here's my Main.cpp:

#include <iostream>
#include "Array.h"
#include "Vector.h"
using namespace std;

int main() {
    cout << "Done" << endl;

    return 0;
}

      

... and everything works fine. However, when I create another .cpp file with only #include ...

DataReader.cpp

#include "Array.h"
#include "Vector.h"

      

... then things blow up and I get tons of errors for every method, constructor and operator overload in Vector:

DataReader.o: In function `Vector':
C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:49: multiple definition of `Vector::Vector()'
TestMain.o:C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:49: first defined here
DataReader.o: In function `Vector':
C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:53: multiple definition of `Vector::Vector(int const&, int const&, int const&)'
TestMain.o:C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:53: first defined here
DataReader.o: In function `Vector':
C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:56: multiple definition of `Vector::Vector(double const&, double const&, double const&)'
TestMain.o:C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:56: first defined here
DataReader.o: In function `ZNK6Vector1xEv':
C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:59: multiple definition of `Vector::x() const'
TestMain.o:C:\C++\Eclipse CDT\workspace\3D_Adaptive_FEM\Debug/..//Vector.h:59: first defined here

etc...

      

But if I only have it #include "Array.h"

in DataReader.cpp then everything will be fine!

What could be wrong with #include "Vector.h"

that doesn't apply to #include "Array.h"

?

Edit: Splitting implementations into .cpp files fixes bugs, but that doesn't explain why I should be doing this for a Vector and not an array.

+3


source to share


1 answer


I suspect you have custom definitions for these functions in your header. For example.

#ifndef VECTOR_H
#define VECTOR_H

class Vector
{
public:
    Vector(int x, int y, int z);
private:
    int m_x, m_y, m_z;
};

Vector::Vector(int x, int y, int z)
    : m_x(x), m_y(y), m_z(z)
{}

#endif

      

Since the definition of a constructor is not embedded in the definition of a class, the compiler does not make it implicit inline

. Now, if you include the same file in multiple translation units (i.e. *.cpp

), the linker will throw exactly the error you see, because each of the files *.cpp

will contain its own constructor definition without marking them as inline functions.

The solution is easy, just put inline

before the constructor declaration:



class Vector
{
public:
    inline Vector(int x, int y, int z);
    // ...
};

// ...

      

Alternatively, if the function body is short, as is the case with the constructor shown above, directly embed the function definition in the class definition, since

class Vector
{
public:
    Vector(int x, int y, int z)
        : m_x(x), m_y(y), m_z(z)
    {}
    // ...
};

// ...

      

+6


source







All Articles