重载 .h 文件中的'+'运算符,链接器错误

Overloading the '+' operator in .h file, linker error

本文关键字:链接 错误 运算符 文件 重载      更新时间:2023-10-16

我无法重载+运算符,也不知道为什么。我尝试了很多解决方案,但都没能解决这个问题。(操作员+=工作良好)

我不明白。你能帮忙吗?

我得到错误:

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);    

但没有定义它。

事实上,当我浏览你发布的其他代码时,我发现这是真的。没有此复制构造函数的定义。