我的类(单独的接口和实现)的未解析运算符C++

unresolved operator for my classes (separate interface and implementation) C++

本文关键字:C++ 运算符 单独 接口 我的 实现      更新时间:2023-10-16

其实我的问题很简单。我正在定义两个单独的类,并在第一个类中重载 + 运算符以接受第二个类作为其操作数。但是存在一个链接问题(现在我省略了成员函数的 const(。请注意,如果我将 + 运算符的实现移动到头文件,问题将得到解决。但是我希望有单独的接口和实现。

下面是文件 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

这是文件 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_;
};

文件球元素.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();
}

第二个类 I 派生自文件 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

及其在文件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_;
}

当我想使用 + 运算符时,出现以下错误:

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  

链接错误是由在声明和定义中使用inline引起的。删除它们。

real operator + (sphereElement2 & oprnd2);

real sphereElement::operator + (sphereElement2 & oprnd2)
{
    return diameter() + oprnd2.boxDiameter();
}