简体   繁体   中英

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)

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.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. On the Main.cpp I just have the following.

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

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. Check your project settings / makefile in this case. Or it has been compiled but the result '.obj' file is not used by the linker.

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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