Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2019: unresolved external symbol "" referenced in function

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

Here is my code

Agent.h:

class Agent
{
public:
    Agent();

void printSelf();

Agent.cpp:

void Agent::printSelf()
{
printf("Agent species=%i\n", species);
for (int i=0;i<mutations.size();i++) {
    cout << mutations[i];
}
}

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?

like image 271
user2472852 Avatar asked Jun 11 '13 01:06

user2472852


People also ask

What kind of an error is an unresolved external reference?

Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.

What is an unresolved external symbol in C++?

So, as a result - an "undefined reference/unresolved external symbol error" happens when the linker cannot find global symbols in the object files.

What is LNK2019 unresolved external symbol?

LNK2019 can occur when a declaration exists in a header file, but no matching definition is implemented. For member functions or static data members, the implementation must include the class scope selector. For an example, see Missing Function Body or Variable.


2 Answers

Most likely cause is that you're not linking in the object created from 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:

  1. The error is occurring in World::processMouse meaning that the source for GLView::processMouse is probably irrelevant.

  2. I find your mixing of printf and cout slightly ... disturbing. You should probably avoid printf for serious C++ programming. It works, but it's mostly intended for legacy C support.

like image 128
paxdiablo Avatar answered Oct 18 '22 18:10

paxdiablo


Observed same issue in Visual studio 2008. Clean, followed by Rebuild worked for me.

like image 37
explorer2020 Avatar answered Oct 18 '22 19:10

explorer2020