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?
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:
The error is occurring in World::processMouse meaning that the source for GLView::processMouse is probably irrelevant.
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.
Observed same issue in Visual studio 2008.
Clean, followed by Rebuild worked for me.
Related
I am working in C++ using VS-19 and having some linker issues I do not understand. I am coming from Java so I am not deep on building and linking plus I am working with kind of "legacy" code here so there might be things I don't grasp at all. I am getting this same error with multiple functions and classes but I am describing only one example here.
So I have a main function which calls the checkConnection function from the same file. Which in turn is referencing a class (myIp) defined in a different file which is in a different folder. When trying to call a function on the myIp object I get a linker error:
error LNK2019: unresolved external symbol "public: int __thiscall myIp::Connect(class AnsiString,int,unsigned int)" (?Connect#TIP##QAEHVAnsiString##HII_N#Z) referenced in function "int __cdecl checkConnection(void)" (?checkConnection##YAHXZ)
Folder Structure:
My main File: ..\projects\c++\ATT\MyApp--> here is all the stuff VS created when I started my new Solution
The myip File to include: ..\projects\c++\utilities--> here is the myIp.h and myIp.cpp files
This is what I have in my VS\Project Properties\VC++ Directories\Include Directories: ..\projects\c++\utilities
The myIp files are not part of a project or solution bound to VS just standalone class implementation.
This is my main code:
#include <myIp.h>
int checkConnection(AnsiString host, int url_port)
{
myIp conn;
if (conn.Connect(host, url_port, 10000))
{
// do magic
}
return 0;
}
int main()
{
AnsiString host = "http://10.20.30.40";
int port = 80;
return checkConnection(host, port);
}
This is the function declaration in myIp.h:
#ifndef classMYIPH
#define classMYIPH
class myIp
{
public:
myIp();
~myIp();
int Connect(AnsiString ipadr, int port, unsigned int
timeoutms=4000, unsigned int tries=1, bool event = false);
};
This is the function I am calling in myIp.cpp:
int myIp::Connect(AnsiString host, int port, unsigned int timeoutms, unsigned int tries, bool event)
{
// do connect
// if connection successfull return 1 else 0
}
myIp(); ~myIp(); lack definitions and you need to complete code or comment them.
I am trying to set a principal amount with class constructors and begin getting this error.
LNK2019 unresolved external symbol "public: __thiscall Bond::Bond(double,double,double,char)" (??0Bond##QAE#NNND#Z) referenced in function _main
Attached my code below. Would appreciate if anybody can point out what the error is. Thanks in advance.
// bond.cpp file
Bond::Bond(double prin, double rat, double yTM, char typ)
{
cout << "I have created a tailored Bond" << endl;
setPrincipal(prin);
}
void Bond::setPrincipal(double prin)
{
principal = prin;
}
double Bond::getPrincipal()
{
return principal;
}
//main.cpp
int main(int argc, const char* argv[])
{
Bond complexBond(999.99, 0.05, 10, 'S');
cout << "complexBond.getPrincipal(): " << complexBond.getPrincipal() << endl;
return 0;
}
//bond.h header file
class Bond
{
public:
Bond();
Bond(double, double, double, char);
void setPrincipal(double);
double getPrincipal();
private:
double principal;
double rate;
double yearsToMaturity;
char paymentType; // 'A'nnual, 'S'emi-Annual, 'Q'uarterly
};
LNK2019 is usually caused by two things:
The source file that contains the definition of the symbol isn't compiled
A function is used but the type or number of the parameters don't match the function definition.
When I copy your code and run it, the error doesn’t appear. So, I guess that bond.h isn’t included in bond.cpp. You just add #include "bond.h" at the top of bond.cpp.
If the solution doesn’t solve your problem, you could refer to Microsoft documents
I keep getting an ununderstood "unresolved externals error from C++ from Visual Studio 2013. Thank you very much for your help so far. I have reduced the code even more to the following form (but the Problem persists):
main.cpp:
#include "Fibonacci.h"
using namespace std;
int main(void){
int RandInteger = 3;
Fibonacci Fib(RandInteger);
}
Fibonacci.h
class Fibonacci{
public:
Fibonacci(int n=0);
protected:
int m_n0, m_n1, m_n;
};
Fibonacci.cpp:
#include "Fibonacci.h"
Fibonacci::Fibonacci(int n){
m_n0 = 0;
m_n1 = 1;
m_n = n;
}
This code produces the following error in Visual Studio 2013:
Error 2 error LNK1120: 1 unresolved externals C:\Dropbox\todo\c++\Exam\Ex2\doesn't work\Exercise 2\fibo1\Fibo1\Debug\Fibo1.exe Fibo1
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Fibonacci::Fibonacci(int)" (??0Fibonacci##QAE#H#Z) referenced in function _main C:\Dropbox\todo\c++\Exam\Ex2\doesn't work\Exercise 2\fibo1\Fibo1\main.obj Fibo1
I persists, but as soon as I replace the line in main.cpp with
Fibonacci Fib();
i.e. I do not pass the integer to the constructor, everything works (well it compiles an does nothing as expected).
Thanks for your help! I am really still stuck.
I finally found my error. Turns out that there is really nothing wrong with the code itself, but I've somehow destroyed my VisualStudio project. I am really not an expert for these things, but here is what worked for me:
create a new empty project in Visual Studio
copy your cpp files (all of them also the *.h files) into the new project folder
add them to this new project by right clicking the project and using "add New item"
I know this is the straightforward way to do it, but I cannot see how I broke the old project (it should be simple enough after all)
Thanks to all of you - thanks especially to otisonoza and Angew, for setting me on the right track that there is nothing wrong with the code (which works on their end), but with the Visual Studio project.
Your main function should return int
void main(void){
Should be
int main(){
EDIT: Thanks to otisonoza in the comments for pointing out that some compilers accept this definition of main. Other than this, I can't see any cause for compiler errors in your system. Are you sure you pasted the code exactly as you wrote it?
Also, what's up with the random tick after your definition of main?
}`
Also, you don't need to have semi-colons after each function in your .cpp file:
Fibonacci::Fibonacci(int na){
m_n0 = 0;
m_n1 = 1;
m_n = 2;
};
int Fibonacci::getNext(int FnM1, int FnM2){
return FnM1 + FnM2;
};
can be
Fibonacci::Fibonacci(int na){
m_n0 = 0;
m_n1 = 1;
m_n = 2;
}
int Fibonacci::getNext(int FnM1, int FnM2){
return FnM1 + FnM2;
}
I'm making a simple game involving a Player class and have made an array to hold players when they've been instantiated. The problem is, every time I run the program, I get LNK 2019 errors-"unresolved external symbol". I am not sure why this is happening or how to get it working properly. Any help would be much appreciated.
Player *player = new Player[9];
for(int i = 0; i< world1.numPlayers;i++) // ADD PLAYERS TO ARRAY
{
Player tempplayer(3,3,5,1);
player[i] = tempplayer;
}
This is the Player Class definition:
#pragma once
class Player
{
public:
Player(int width, int height, int xPos, int health );
~Player(void);
Player();
int getWidth();
int getHeight();
int getHealth();
int getPos();
void setHealth(int newHealth);
bool isAlive();
int width;
int height;
int health;
int xPos;
};
Here is the exact warning: "Error 1 error LNK2019: unresolved external symbol "public: __thiscall Player::Player(void)" (??0Player##QAE#XZ) referenced in function _main C:\Users\Alex\Documents\Visual Studio 2012\Projects\ConsoleApplication2\ConsoleApplication2\World.obj ConsoleApplication2"
Your array loop is fine.
The problem is that you didn't define in a .cpp file one or more of the class member functions that you've declared up there in your .hpp file.
We can't tell which one, because you didn't show the whole error, but ensure that they are all defined, even if only with empty definitions.
A common culprit is the destructor; when you don't need it to do anything, it's easy to forget to define it:
Player::~Player()
{
}
It may also be your default constructor:
Player::Player()
{
}
Define them all!
Martin is right that you need to assign pointers to that array. I would have thought that such an error would have given you compile time warnings.
To the linker error - The linker warning usually prints what it is unable to find. That is usually sufficient to pin point the source of error, or at least point in the right direction. Can you paste the complete warning?
Usually the link time errors are a result of undefined member functions, as mentioned in the comment by dreamlax, OR failure to include one of the libraries.
You use of class pointers, instances and constructors seems kinda off.
This is what your code should look like:
Player *player[9];
for(int i = 0; i< world1.numPlayers;i++) // ADD PLAYERS TO ARRAY
{
player[i] = new Player(3,3,5,1);
}
EDIT: Note that this only bypasses the cause of the original link error. The linker is complaining that the default constructor, Player::Player(void), cannot be found. If I'm not mistaken, its being called when you create your array with new Player[9]. Declaring it should solve this error, allowing you to keep using the original code instead of my version.
Greetings.
I have searched for a solution, but I think this problem is personal code specific, hence my posting here.
I'll get straight to the point.
In my main I have two objects.
Computer *computer = new Computer();
Player *player = new Player();
In the computer class, in the header I have the following:
private:
Strategy *strategy;
int winningPosition;
int twoInRow;
int counter;
int makeTwo;
Then in Computer.cpp:
Computer::Computer(char token, bool hasTurn)
{
m_token = token;
m_hasTurn = hasTurn;
strategy = new Strategy();
}
int Computer::getMove(const char (&board)[9])
{
twoInRow = strategy->checkTwoInRow(board);
counter = strategy->counter(board);
makeTwo = strategy->makeTwo(board);
if(twoInRow != 0)
{
return twoInRow - 1;
} else if(counter != 0) {
return counter - 1;
} else if(makeTwo != 0) {
return makeTwo - 1;
} else {
return 0;
}
}
At this point I think the problem arises.
The methods called from the class Strategy all require knowledge of the board thus:
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
The problems im getting, unabling them to compile:
Error 1 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::makeTwo(char const (&)[9])" (?makeTwo#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 2 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::counter(char const (&)[9])" (?counter#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
Error 3 error LNK2019: unresolved external symbol "public: int __thiscall Strategy::checkTwoInRow(char const (&)[9])" (?checkTwoInRow#Strategy##QAEHAAY08$$CBD#Z) referenced in function "public: int __thiscall Computer::getMove(char const (&)[9])" (?getMove#Computer##QAEHAAY08$$CBD#Z) C:\CPP\TTT\Computer.obj tictactoeCPP
As a c++ noob, I have absolutely no clue why or how this problem is caused. I think it has to do something with either the instantiation of Strategy in the computer class, or with the parameter given from computer to the strategy in the method calls.
Can anyone explain WHY this error is occuring, I don't quite understand the error at all.
And also how to solve/prevent this?
EDIT*
I just got a request to share the Strategy class:
Strategy.h:
#pragma once
class Strategy
{
public:
Strategy(void);
~Strategy(void);
int checkTwoInRow(const char (&board)[9]);
int counter(const char (&board)[9]);
int makeTwo(const char (&board)[9]);
};
The class defined these methods, I won't post them because they are quite long.
This is a linking error and it has nothing to do with instantiations or parameters.
You haven't provided the linker with the definitions for those functions. If you defined them in Strategy.cpp you need to compile that and add it as an argument to the linker. How you do that depends entirely on what tools you're using to build your program.
If you're using Visual Studio (or any other IDE) it should take care of it automatically once you've added Strategy.cpp to your project.
Or did you perhaps define them like this:
int checkTwoInRow(const char (&board)[9])
{
// Do something with board the wrong way
}
instead of like this:
int Strategy::checkTwoInRow(const char (&board)[9])
{
// Do something with board the right way
}
The first one doesn't define a Strategy member function, it defines a global function.
The error is simply stating that you have declared, but not defined, the member functions Strategy::makeTwo, Strategy::counter, and Strategy::checkTwoInRow. Are you sure that you implemented them (in a source file that's actually being compiled) and that you didn't accidentally define them as free functions?