Actually my question is simple. I am defining two separate classes and I overload + operator in the first class to accept the second class as its operand. But there is a linking problem (for now I am omitting const for member function). Please note that if I move the implementation of the + operator to the header file, the problem will be solved. But I want to have separate interface and implementation.
Here is the base class in file elementBase.h:
typedef double real;
#ifndef __elementBase_H__
#define __elementBase_H__
class elementBase
{
public:
// return the sphere diameter enclosing the whole element
virtual real boxDiameter() = 0;
// return the longest size of the element
virtual real longestSize() = 0;
// return the smallest size of the element
virtual real smallestSize() = 0;
protected:
};
#endif
this is the first derived class in file sphereElement.h:
#ifndef __sphereElement_H__
#define __sphereElement_H__
#include "elementBase.h"
#include "sphereElement2.h"
class sphereElement : public elementBase
{
public:
// constructors
// null constructor
sphereElement();
// make the element with its diameter
sphereElement(real d);
// return the sphere radius enclosing the whole element
virtual real boxDiameter();
// return the diameter of the sphere
inline virtual real diameter();
inline real operator + (sphereElement2 & oprnd2);
protected:
real diam_;
};
the implementation in file sphereElement.cpp:
#include "sphereElement.h"
sphereElement::sphereElement() : diam_(0)
{
}
// make the element with its diameter
sphereElement::sphereElement(real d) : diam_(d)
{
}
// return the sphere radius enclosing the whole element
real sphereElement::boxDiameter()
{
return diam_;
}
inline real sphereElement::diameter()
{
return diam_;
}
inline real sphereElement::operator + (sphereElement2 & oprnd2)
{
return diameter() + oprnd2.boxDiameter();
}
The second class I derived from base class in file sphereElement2.h:
#ifndef __sphereElement2_H__
#define __sphereElement2_H__
#include "elementBase.h"
class sphereElement2 : public elementBase
{
public:
// constructors
// null constructor
sphereElement2();
// make the element with its diameter
sphereElement2(real d);
// return the sphere radius enclosing the whole element
virtual real boxDiameter();
protected:
real diam_;
};
#endif
and its implementation in file sphereElement2.cpp:
#include "sphereElement2.h"
#include "constants.h"
sphereElement2::sphereElement2() : diam_(0)
{
}
// make the element with its diameter
sphereElement2::sphereElement2(real d) : diam_(d)
{
}
// return the sphere radius enclosing the whole element
real sphereElement2::boxDiameter()
{
return diam_;
}
when I want to use the + operator, I get the following error:
sphereElement SS1(4);
sphereElement2 SS2(5);
cout<< SS1 + SS2 << endl;
error: Error 1 error LNK2019: unresolved external symbol "public: double __thiscall sphereElement::operator+(class sphereElement2 &)" (?? HsphereElement@@QAENAAVsphereElement2@@@Z) referenced in function _wmain