C++ 错误 LNK2019:函数中引用了未解析的外部符号“”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17035217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error LNK2019: unresolved external symbol "" referenced in function
提问by user2472852
Im currently getting the following error when I compile my code:
当我编译我的代码时,我目前收到以下错误:
error LNK2019: unresolved external symbol "public: void __thiscall Agent::printSelf(void)" (?printSelf@Agent@@QAEXXZ) referenced in function "public: void __thiscall World::processMouse(int,int,int,int)" (?processMouse@World@@QAEXHHHH@Z) World.obj
错误 LNK2019:未解析的外部符号“public: void __thiscall Agent::printSelf(void)” (?printSelf@Agent@@QAEXXZ) 在函数“public: void __thiscall World::processMouse(int,int,int,int)”中引用(?processMouse@World@@QAEXHHHH@Z) World.obj
Here is my code
这是我的代码
Agent.h:
代理.h:
class Agent
{
public:
Agent();
void printSelf();
Agent.cpp:
代理.cpp:
void Agent::printSelf()
{
printf("Agent species=%i\n", species);
for (int i=0;i<mutations.size();i++) {
cout << mutations[i];
}
}
GLView.cpp:
GLView.cpp:
void GLView::processMouse(int button, int state, int x, int y)
{
if(world->isDebug()) printf("MOUSE EVENT: button=%i state=%i x=%i y=%i\n", button, state, x, y);
if(button==0){
int wx= (int) ((x-conf::WWIDTH/2)/scalemult-xtranslate);
int wy= (int) ((y-conf::WHEIGHT/2)/scalemult-ytranslate);
world->processMouse(button, state, wx, wy);
}
mousex=x; mousey=y;
downb[button]=1-state;
}
void World::processMouse(int button, int state, int x, int y)
{
if (state==0) {
float mind=1e10;
float mini=-1;
float d;
for (int i=0;i<agents.size();i++) {
d= pow(x-agents[i].pos.x,2)+pow(y-agents[i].pos.y,2);
if (d<mind) {
mind=d;
mini=i;
}
}
if (mind<1000) {
//toggle selection of this agent
for (int i=0;i<agents.size();i++) {
if(i!=mini) agents[i].selectflag=false;
}
agents[mini].selectflag= !agents[mini].selectflag;
agents[mini].printSelf();
setControl(false);
}
}
}
}
Im pretty stumped. I haven't worked on this code in a long time, so im not sure what has changed to cause this. Anyone see anything wrong?
我很难过。我很长时间没有处理过这段代码,所以我不确定是什么改变了导致这种情况。有没有人看到有什么不对?
回答by paxdiablo
Most likely cause is that you're not linking in the object created from Agent.cpp
.
最可能的原因是您没有链接从Agent.cpp
.
You should check to ensure that it's part of the project and that you're using the correct version, compiled with this current compiler as well (since you state you haven't touched it in a while, it may be that the objects were built with an earlier compiler version, potentially making them incompatible - different name mangling methods, for example).
您应该检查以确保它是项目的一部分,并且您使用的是正确的版本,也使用当前的编译器编译(因为您声明您有一段时间没有接触它,可能是构建了对象与较早的编译器版本,可能使它们不兼容 - 例如不同的名称修改方法)。
The first thing to try (once you've established all correct files are in the project) is a full clean-and-build.
首先要尝试(一旦您在项目中建立了所有正确的文件)是完整的清理和构建。
On a few other points:
关于其他几点:
The error is occurring in
World::processMouse
meaning that the source forGLView::processMouse
is probably irrelevant.I find your mixing of
printf
andcout
slightly ... disturbing. You should probably avoidprintf
for serious C++ programming. It works, but it's mostly intended for legacy C support.
错误的发生
World::processMouse
意味着来源GLView::processMouse
可能无关紧要。我发现你的混合
printf
和cout
轻微......令人不安。您可能应该避免printf
进行严肃的 C++ 编程。它可以工作,但主要用于遗留 C 支持。
回答by New-bie
Observed same issue in Visual studio 2008. Clean, followed by Rebuild worked for me.
在 Visual Studio 2008 中观察到同样的问题。清理,然后重建对我有用。