我的类(单独的接口和实现)的未解析运算符C++
unresolved operator for my classes (separate interface and implementation) C++
其实我的问题很简单。我正在定义两个单独的类,并在第一个类中重载 + 运算符以接受第二个类作为其操作数。但是存在一个链接问题(现在我省略了成员函数的 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();
}
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 如何防止clang格式在流运算符调用之间添加换行符<<