简体   繁体   English

尝试重载=运算符时出现1个未解决的外部错误

[英]1 unresolved externals error when trying to overload= operator

I have a custom class and am trying to create an operator overload function for =. 我有一个自定义类,正在尝试为=创建运算符重载函数。 Unfortunately the error I get does not point to a specific line or error I just get the following error. 不幸的是,我得到的错误没有指向特定的行或错误,我只是得到以下错误。

unresolved external symbol "public: int __thiscall sequence::size(void)const " (?size@sequence@@QBEHXZ) referenced in function "public: void __thiscall sequence::operator=(class sequence const &)" (??4sequence@@QAEXABV0@@Z) 未解析的外部符号“ public:int __thiscall sequence :: size(void)const”(?size @ sequence @@ QBEHXZ)在函数“ public:void __thiscall sequence :: operator =(class sequence const&)”中引用(?4sequence @@ QAEXABV0 @@ Z)

It is on file Sequence2.obj line 1. As that is not a file I edit, I am a little unsure what the error is in the function. 它位于文件Sequence2.obj的第1行上。由于这不是我编辑的文件,因此我有点不确定函数中的错误。

Sequence2.cpp Sequence2.cpp

void sequence::operator=(const sequence & source)
{
    if (size() <= source.size()) {

        delete[] data;

        data = new value_type[source.size()];
        current_index = -1;
        used = 0;
    }
    else {

        delete[] data;

        data = new value_type[source.size() * 2];
        current_index = -1;
        used = 0;
    }

    for (int i = 0; i < source.size(); i++)
    {
        data[i] = source.data[i];
        used++;
        current_index++;
    }
}

The size function, just returns the size of the sequence. size函数仅返回序列的大小。 On the Main.cpp I just have the following. 在Main.cpp上,我只有以下内容。

sequence test; // A sequence that we’ll perform tests on
sequence test2;

test.insert(33);
test.insert(35);
test.insert(36);

test2 = test;

The "unresolved external" error happens during the link step of compilation. 在编译的链接步骤中发生“未解决的外部”错误。 See this question for more detail. 有关更多详细信息,请参见此问题 Note that in compilation step the compiler made Sequence2.obj from Sequence2.cpp (and everthing it includes). 请注意,在编译步骤中,编译器从Sequence2.cpp (及其包含的所有内容)中创建了Sequence2.obj

And yes, linker errors are a bit trickier than those compiler errors that happen during the actual compilation of source code. 是的,链接器错误比实际编译源代码期间发生的那些编译器错误要棘手。

I guess that somewhere in your code you have something like 我猜你在代码的某处有类似

class sequence
{
    // ... some declarations

    int size();

    // ... more declarations
}

but there is no corresponding 但是没有对应的

int sequence::size()
{
    // implementation of size()
}

Or maybe there is an implementation of size() but it is not compiled. 也许有一个size()的实现,但是它没有被编译。 Check your project settings / makefile in this case. 在这种情况下,请检查项目设置/ makefile。 Or it has been compiled but the result '.obj' file is not used by the linker. 或者它已被编译,但是链接器未使用结果“ .obj”文件。

The error message at least claims the linker does not know about an .obj file that contains the translated (ie compiled) counterpart of the implementation of size() . 该错误消息至少声明链接器不知道.obj文件,该文件包含size()实现的已翻译(即,已编译)副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM