重载 .h 文件中的'+'运算符,链接器错误
Overloading the '+' operator in .h file, linker error
我无法重载+运算符,也不知道为什么。我尝试了很多解决方案,但都没能解决这个问题。(操作员+=工作良好)
我不明白。你能帮忙吗?
我得到错误:
LNK2019:未解析的外部符号"public:__thiscallZbior::Zbior(类Zbior常量&)"(??0$Zbior@H@@QAE@ABV0@@Z) 在函数"public:class"中引用Zbior __thiscall Zbior::运算符+(类Zbior const&)"(??H$Zbior@H@@QAE?AV0@ABV0@@Z)
当我更改方法的返回对象时,问题不存在:Zbior运算符+(常量Zbior和其他)
至
return Zbior<T>();
我的.h文件是:
template<class T>
class Zbior
{
public:
Zbior(void);
~Zbior(void);
void dodajElement(T elem);
Zbior<T>(vector<T> elementyZbioru);
Zbior<T>(Zbior<T> const &other);
Zbior<T>& operator+=(Zbior<T> const &other);
Zbior<T> operator+ (const Zbior<T> &other)
{
vector<T> otherVec = other.elementyZbioru;
vector<T> thisVec = elementyZbioru;
Zbior<T> wyjZbior = Zbior<T>();
for (vector<T>::iterator p = otherVec.begin(); p != otherVec.end(); ++p)
{
wyjZbior.dodajElement(*p);
}
for (vector<T>::iterator p = thisVec.begin(); p != thisVec.end(); ++p)
{
wyjZbior.dodajElement(*p);
}
return Zbior<T>(wyjZbior);
}
private:
vector<T> elementyZbioru;
};
我的.cpp文件是:
#include "Zbior.h"
template<class T>
Zbior<T>::Zbior(void)
{
}
template<class T>
Zbior<T>::~Zbior(void)
{
}
template<class T>
Zbior<T>::Zbior(vector<T> elementyZbioru)
{
this->elementyZbioru = elementyZbioru;
}
template<class T>
void Zbior<T>::dodajElement(T elem){
this->elementyZbioru.push_back(elem);
}
template<class T>
Zbior<T>& Zbior<T>::operator+=(Zbior<T> const &inny){
vector<T> innyElementyZbioru = (inny.elementyZbioru);
for (vector<T>::iterator p = innyElementyZbioru.begin(); p != innyElementyZbioru.end(); ++p)
{
dodajElement(*p);
}
return *this;
}
同类产品:
#include "stdafx.h"
#include "Zbior.cpp"
#include "Zbior.h"
int _tmain(int argc, _TCHAR* argv[])
{
Zbior<int> zb1 = Zbior < int>();
zb1.dodajElement(1);
zb1.dodajElement(1312);
Zbior<int> zb2 = Zbior < int>();
zb2.dodajElement(21);
zb2.dodajElement(21312);
//zb1 += zb2;
Zbior<int> zb = zb1 + zb2;
//Zbior<Zbior<int>> zbzb = Zbior<Zbior<int>> ();
//zbzb.dodajElement(zb1);
//zbzb.dodajElement(zb2);
system("PAUSE");
return 0;
}
您声明了一个复制构造函数
Zbior<T>(Zbior<T> const &other);
但不要在任何地方定义它。由于该类在复制时不包含任何需要特别注意的成员,因此只需删除该声明——隐式生成的复制构造函数和赋值运算符将执行正确的操作。
(如果您使用.cpp
以外的文件扩展名作为包含模板实现的头,可能会更好,因为这会导致包括我在内的一些人认为错误是将实现放在源文件中。或者只是将实现与类模板定义放在同一个头中。)
"未解决的外部错误"表示您已经声明并尝试使用某个函数,但尚未提供该函数的定义。有点像这样:
int foo();
int main()
{
int n = foo();
return n;
}
foo
已经被声明和调用(在main
中),但还没有实现
您可以实现这个构造函数,也可以删除声明,因为您不需要它中的任何特殊行为。
再看看你的错误信息:
LNK2019:未解析的外部符号"public:__thiscallZbior::Zbior(类Zbior const&)"(??0$Zbior@H@@QAE@ABV0@@Z) 在函数"public:class Zbior __thiscall"中引用Zbior::运算符+(类Zbior const&)"(??H$Zbior@H@@QAE?AV0@ABV0@@Z)
这是在告诉你你已经申报:
template<class T>
class Zbior
{
public:
[...]
Zbior<T>(Zbior<T> const &other);
但没有定义它。
事实上,当我浏览你发布的其他代码时,我发现这是真的。没有此复制构造函数的定义。
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误