This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
Hi I am noob with classes and I just started learning them. I need help with this code as I have no idea why I get this error.
Here is my Code
main.cpp
#include "stdafx.h"
#include "player.h"
SDL_Surface* screen = NULL;
void init()
{
SDL_Init( SDL_INIT_VIDEO );
//Set up screen
screen = SDL_SetVideoMode( 640, 480, 32, SDL_DOUBLEBUF |SDL_HWSURFACE |SDL_SWSURFACE);
//SDL_BlitSurface( hello, NULL, screen, NULL );
_putenv("SDL_VIDEO_CENTERED=1"); // Center the window
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("SDL Animation", "SDL Animation");
//screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_DOUBLEBUF |SDL_HWSURFACE |SDL_SWSURFACE);
SDL_EnableKeyRepeat(0, 1600000);
if(screen == NULL){
printf("SDL_Init failed: %s\n", SDL_GetError());
}
}
int _tmain(int argc, _TCHAR* argv[])
{
init();
//SDL_Surface* sprite = SDL_BlitSurface(sprite,rcSprite, screen, &rcSprite);
Player player;
//Update Screen
SDL_Flip( screen );
SDL_FreeSurface(screen);
return 0;
}
player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_TTF.h"
class Player
{
public:
Player (){
HP = 20;
playerScore = 0;
playerSpeed = 10;
playerJump = 20;
}
~Player();
int getScore() { return playerScore;}
void setSpeed(int speed) { int playerSpeed = speed;}
void setJump (int jump) {playerJump = jump;}
void movePlayer(int x ,int y);
void runRightAnim (SDL_Rect* clip);
void runLeftAnim (SDL_Rect* clip);
void drawPlayer(int x, int y, SDL_Surface* source, SDL_Surface* destination);
static SDL_Rect sprite;
private:
static int HP;
static int playerScore;
static int playerSpeed;
static int playerJump;
static int animRate;
};
#endif
and the last one player.cpp
#include "stdafx.h"
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_TTF.h"
#include "player.h"
void Player::runRightAnim(SDL_Rect* clip)
{
}
void Player::runLeftAnim(SDL_Rect* clip)
{
}
void Player::drawPlayer(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect dst;
dst.x = x;
dst.y = y;
SDL_BlitSurface(source,NULL,destination,&dst);
//SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);
}
and the error is
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Player::~Player(void)" (??1Player##QAE#XZ) referenced in function _wmain
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::playerJump" (?playerJump#Player##0HA)
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::playerSpeed" (?playerSpeed#Player##0HA)
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::playerScore" (?playerScore#Player##0HA)
1>main.obj : error LNK2001: unresolved external symbol "private: static int Player::HP" (?HP#Player##0HA)
The code is obviously not finished but I just want to get rid of this error
"unresolved external" errors occur when you declare and attempt to use a function, but you don't implement it. In particular:
1>main.obj : error LNK2019: unresolved external symbol "public:
__thiscall Player::~Player(void)" (??1Player##QAE#XZ) referenced in function _wmain
Is because though you have a declaration for Player::~Player():
class Player
{
public:
/* ... */
~Player();
This method is never implemented.
Fix this error by implementing the methods in question.
Edit: Additionally, your class has static member variables which are declared but never defined. Your declaration is here:
class Player
{
/* ... */
private:
static int HP;
static int playerScore;
static int playerSpeed;
static int playerJump;
static int animRate;
};
But this declaration does not define the variables. You must define and initialize these statics, else you will get (are getting) unresolved externals here as well.
Generally somewhere in the cpp file:
int Player::playerScore = 0;
As an aside, you are using static member variables for a class which appears to be instantiatable multiple times. If you do this:
Player a;
Player b;
to instantiate multiple Player objects, because the member variables are declared static they will each "share" the same playerScore etc. This doesn't make a lot of sense when looking at your code. You probably won't want these to be statics.
Related
#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),0){};
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier.Deallocate(myChar, 1);
return 0;
}
The loki library uses essentially generic programming in c++
I have got the code up there using Small object allocator of memory(Loki::SmallObjAllocator)
I m using visual c++ 2010
I get those errors:
> MyLoki.cpp
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void __thiscall Loki::SmallObjAllocator::Deallocate(void *,unsigned int)" (?Deallocate#SmallObjAllocator#Loki##QAEXPAXI#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void * __thiscall Loki::SmallObjAllocator::Allocate(unsigned int,bool)" (?Allocate#SmallObjAllocator#Loki##QAEPAXI_N#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "protected: __thiscall Loki::SmallObjAllocator::SmallObjAllocator(unsigned int,unsigned int,unsigned int)" (??0SmallObjAllocator#Loki##IAE#III#Z) referenced in function "public: __thiscall MySmallObj::MySmallObj(void)" (??0MySmallObj##QAE#XZ)
I have founded an answer to my first question.
#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),1){}; //the chunkSize < maxObjsize
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier->Deallocate(myChar, 1);
return 0;
}
the errors of multiple
unresolved external symbol
are because of SmallObj.cpp , of Loki that was not included to the project
for example of Loki::SmallObjAllocator, Loki::SmallObject here
EDIT: I believe my issue is because I'm calling my FacePoint class from an .exe project, and FacePoint is actually located in a .dll project, both within the same solution. Any idea to combat this?
I have a class called FacePoint which gets and sets X and Y values of a face feature.
This is FacePoint.h:
#pragma once
class FacePoint
{
public:
FacePoint(void);
FacePoint(int X, int Y);
void FacePoint::SetX(int X);
void FacePoint::SetY(int Y);
void FacePoint::SetXY(int X, int Y);
int FacePoint::GetX();
int FacePoint::GetY();
~FacePoint(void);
private:
int coordX;
int coordY;
};
...and FacePoint.cpp:
#include "StdAfx.h"
#include "FacePoint.h"
FacePoint::FacePoint(void)
{
coordX = 0;
coordY = 0;
}
FacePoint::FacePoint(int X, int Y)
{
coordX = X;
coordY = Y;
}
void FacePoint::SetX(int X){ coordX = X; }
void FacePoint::SetY(int Y){ coordY = Y; }
void FacePoint::SetXY(int X, int Y)
{
coordX = X;
coordY = Y;
}
int FacePoint::GetX(){ return coordX; }
int FacePoint::GetY(){ return coordY; }
FacePoint::~FacePoint(){}
This seems OK to me, however when I try to implement a vector of FacePoint objects, as follows:
std::vector<FacePoint> point;
result = GetFeaturePoints(pHandle, &point);
int x = 0;
int y = 0;
for(int p = 0; p < point.size(); p++)
{
x = point[p].GetX();
y = point[p].GetY();
}
I get the following compile-time errors:
Error 37 error LNK1120: 3 unresolved externals
Error 36 error LNK2001: unresolved external symbol "public: __thiscall
FacePoint::~FacePoint(void)" (??1FacePoint##QAE#XZ)
Error 35 error LNK2001: unresolved external symbol "public: int
__thiscall FacePoint::GetX(void)" (?GetX#FacePoint##QAEHXZ)
Error 34 error LNK2001: unresolved external symbol "public: int
__thiscall FacePoint::GetY(void)" (?GetY#FacePoint##QAEHXZ)
I'm not sure why this is happening, but from the error messages, I suspect it's something to do with my destructor?...
I searched around and most of these issues were around missing a constructor/destructor definition. To my knowledge, my constructor seems fine, and the error message seems to hint at the destructor. The only thing I can think of is I'm missing a desctructor which destroys int X, int Y, but I didn't think primitives needed that?...
Any help is appreciated. Thank you!!
EDIT: So I forgot to #include "FacePoint.h" in my main (where I am creating the vector of FacePoint). I added that in, but am receiving this compile-time error message:
Error 4 error LNK2019: unresolved external symbol "public: __thiscall FacePoint::~FacePoint(void)" (??1FacePoint##QAE#XZ) referenced in function "public: void * __thiscall FacePoint::`scalar deleting destructor'(unsigned int)" (??_GFacePoint##QAEPAXI#Z)
Any idea, guys? Thanks!
It looks like FacePoint.cpp is not part of your project. That would mean that the linker can't find the body of code that implements those functions.
Solution by OP.
Solved by replacing the class with a struct.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have created this code
class Game{
static SDL_Surface* screen;
public:
//Initiate Game(SDL_Graphics, folder for output.....)
static void initialize();
static void initializeScreen();
};
void Game::initializeScreen()
{
Game::screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_DOUBLEBUF |SDL_HWSURFACE |SDL_SWSURFACE);
SDL_Init(SDL_INIT_VIDEO);
Game::screen == NULL ? printf("SDL_Init failed: %s\n", SDL_GetError()):printf("SDL_Init initialized\n");
SDL_WM_SetCaption("SDL Animation", "SDL Animation");
}
It compiles but I get e linker error, how can I fix this?
1>game.obj : error LNK2001: unresolved external symbol "private: static struct SDL_Surface * Game::screen" (?screen#Game##0PAUSDL_Surface##A)
Edit: This is how I fixed it, in game.cpp
added this
SDL_Surface* Game::screen;
outside of any function*
You have to define static (variable) members (not functions) in seperate source files(*.cpp) and link against them.
for example:
//MyClass.h
class MyClass {
static int x;
};
//MyClass.cpp
#include "MyClass.h"
int MyClass::x;
You need to add the following definition in your cpp file SDL_Surface* Game::screen = NULL.
Here is an example code where you can have a static variable without defining it in the cpp file using a function.
#include <iostream>
struct Lazy {
static int& GetValue() {
static int a = 0;
return a;
}
};
int main() {
std::cout << Lazy::GetValue() << std::endl;
int& a = Lazy::GetValue();
a = 1;
std::cout << Lazy::GetValue() << std::endl;
}
I'm having a problem with a vector declaration.
Here's the code:
.h
#ifndef ANIMATEDSPRITE_H_
#define ANIMATEDSPRITE_H_
#include "Sprite.h"
#include <vector>
//using namespace std;
class AnimatedSprite //abstract class to point sprites
{
public:
AnimatedSprite();
~AnimatedSprite();
//gets and sets
Sprite GetMySprite(int _index);
void SetSpriteToList(Sprite _sprite);
int GetState() const;
void SetState(int _state);
//other
private:
std::vector<Sprite> spriteList;
int state; //estado que esse sprite representa (parado esquerda, andando direita, etc)
};
#endif
.cpp
#include "AnimatedSprite.h"
AnimatedSprite::AnimatedSprite()
{
spriteList.clear();
state = NULL;
}
AnimatedSprite::~AnimatedSprite()
{
}
Sprite AnimatedSprite::GetMySprite(int _index)
{
return spriteList[_index];
}
void AnimatedSprite::SetSpriteToList( Sprite _sprite )
{
//Sprite* temp = new Sprite(1,2);
spriteList.push_back(_sprite);
}
int AnimatedSprite::GetState() const
{
return state;
}
void AnimatedSprite::SetState( int _state )
{
state = _state;
}
But I'm getting 2 errors:
Error 1 error LNK2019: unresolved external symbol imp_CrtDbgReportW referenced in function "public: class Sprite & __thiscall std::vector >::operator[](unsigned int)" (??A?$vector#VSprite##V?$allocator#VSprite###std###std##QAEAAVSprite##I#Z) AnimatedSprite.obj
Error 2 fatal error LNK1120: 1 unresolved externals C:\DevProjects\SDLSkeleton\Debug\SDLSkeleton.exe
I've found a solution removing the _DEBUG from the Preprocessor Definitions, but it seems kinda wrong to do that.
Is it the right solution? What's the consequence of removing it?
In the book and documentations I've checked it should be just a common variable declaration, but this errors showed up.
Thanks.
This is because your build is inconsistent: you define _DEBUG macro, but link with release CRT version (/MD). So either remove _DEBUG, or select /MDd option.
I have an very strange error: when I want to use the SocialServer::Client class from my SocialServer::Server class, the linker threw me two LNK2019 errors :
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall SocialServer::Client::Handle(void)" (?Handle#Client#SocialServer##QAEXXZ) referenced in function "private: static unsigned int __stdcall SocialServer::Server::listenThread(void *)" (?listenThread#Server#SocialServer##CGIPAX#Z) C:\Users\benjamin\Documents\Visual Studio 2010\Projects\FCX Social Server\SocialServer Core\Server.obj SocialServer Core
Error 2 error LNK2019: unresolved external symbol "public: __thiscall SocialServer::Client::Client(unsigned int)" (??0Client#SocialServer##QAE#I#Z) referenced in function "private: static unsigned int __stdcall SocialServer::Server::listenThread(void *)" (?listenThread#Server#SocialServer##CGIPAX#Z) C:\Users\benjamin\Documents\Visual Studio 2010\Projects\FCX Social Server\SocialServer Core\Server.obj SocialServer Core
However , these 2 missing function are correctly implemented :
Client.h
#pragma once
#include "dll.h"
namespace SocialServer
{
class __social_class Client
{
public:
Client(SOCKET sock);
~Client();
void Handle();
private:
static unsigned __stdcall clientThread(void* value);
SOCKET _socket;
uintptr_t _thread;
unsigned int _thread_id;
};
}
Client.cpp
#pragma once
#include "Client.h"
namespace SocialServer
{
Client::Client(SOCKET socket)
{
this->_socket = socket;
}
Client::~Client()
{
}
void Client::Handle()
{
std::cout << " New client " << std::endl;
this->_thread = _beginthreadex(NULL, 0, Client::clientThread, &this->_socket, CREATE_SUSPENDED, &this->_thread_id);
ResumeThread((HANDLE)this->_thread);
}
unsigned __stdcall Client::clientThread(void* value)
{
// Some code to execute here ...
}
}
Where does the problem comes from ?
i've found the solution.
In a function that's used by _beginthreadex() (with unsigned __stdcall) , always add a return at the end.