找到你要的答案

Q:c++ LNK2001 & LNK1120 compile errors [duplicate]

Q:C++编译错误LNK2001和lnk1120 [重复]

I am debugging some C++ software, and want to modify an existing function slightly, so that it changes the value of a particular variable used elsewhere in the program.

The function is currently defined as so:

void DataStore::setEraseSelected(){
    ...
    // Function code here
    ...
}

As it stands, the function works correctly with no problems whatsoever. But, I now want to modify the function, so that it will change the value of a variable used elsewhere in the program. To do this, I have tried passing the variable into the function as a parameter (I have also updated the header file to reflect the changes to the function), and then assigning a value to the variable inside the function:

void DataStore::setEraseSelected(toAMS::DataMessage statusMsg){
    ...
    // Function code here
    ...
    statusMsg.CODE_ERASE = Types::Activated;
    ...
}

As mentioned, I have added the declaration to the header file, so that it now has the declaration for the function with the parameter, as well as the one for the function without the parameter:

void DataStore::setEraseSelected(toAMS::DataMessage statusMsg);

But when I try and build the code (using Visual Studio 2010), I get the following two compile errors:

error LNK2001: unresolved external symbol "public void_thiscall DataStore::setEraseSelected(void)" (? setEraseSelected@DataStore::QAEXXZ)

error LNK1120: 1 unresolved externals

The first error highlights the project .obj file, which I have tried deleting and building again, but get the same error, and second one highlights the project .exe file, which I have also tried deleting and building again, but get the same error.

Anyone have any ideas why? I've had a look on SO for questions regarding these errors, but none of them seem to clearly explain why I might be getting them. They all seem to suggest that the compiler is possibly looking in the wrong place, but if I undo my changes, then the code compiles with no problems, and I haven't told the compiler to look anywhere else when building the code with my changes...

这个问题在这里已经有了答案:

我有些C++软件调试,并要修改现有的功能点,所以它的变化在程序的其他地方使用某个变量的值。

函数当前定义为:

void DataStore::setEraseSelected(){
    ...
    // Function code here
    ...
}

它的功能正常工作,没有任何问题。但是,我现在想修改函数,这样它将改变程序中其他地方使用的变量的值。为此,我尝试将变量作为参数传递给函数(我也更新了头文件以反映函数的变化),然后将值赋给函数内的变量:

void DataStore::setEraseSelected(toAMS::DataMessage statusMsg){
    ...
    // Function code here
    ...
    statusMsg.CODE_ERASE = Types::Activated;
    ...
}

如前所述,我已将声明添加到头文件中,以便它现在具有参数的函数声明,以及没有参数的函数的声明:

void DataStore::setEraseSelected(toAMS::DataMessage statusMsg);

但当我尝试建立代码(使用Visual Studio 2010),我得到以下两个编译错误:

错误LNK2001:未解决的外部符号”的公共void_thiscall数据存储::seteraseselected(void)”(?seteraseselected @数据存储::qaexxz)

错误lnk1120:1未解决的疑团

第一个错误的亮点工程。obj文件,我试着删除和重新建设,但得到同样的错误,和第二个亮点项目。exe文件,我也试着删除和重新建设,但得到同样的错误。

有人有什么想法吗?我有一个关于这些错误的问题,但他们似乎没有清楚地解释为什么我可能得到他们。他们似乎都认为编译器可能在错误的地方,但如果我撤消我的变化,然后代码编译没有问题,我还没有告诉编译器寻找任何地方时,建立代码与我的变化…

answer1: 回答1:

void DataStore::setEraseSelected(int );

Pass that variable

无效的数据存储::seteraseselected(int);

通过变量

c++  lnk2001  lnk