I suspect you have out-of-line definitions of these functions in you header. E.g.
#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 the constructor is not inline in the class definition, the compiler does not make it implicitly inline
. Now, if you include the same file in multiple translation units (i.e. *.cpp
files), the linker will produce exactly the error you see, because each of the *.cpp
files will contain its own definition of the constructor without them being marked as inline functions.
The solution is easy, just put a inline
in front of 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 inline the function definition into the class definition, as
class Vector
{
public:
Vector(int x, int y, int z)
: m_x(x), m_y(y), m_z(z)
{}
// ...
};
// ...