2014-03-30 143 views
1

我已经使用SDL编写了此代码,但存在一些错误。解决错误LNK2019;无法解析的外部符号

这是代码

#include <SDL.h> 
#include <SDL_image.h> 
#include <SDL_ttf.h> 
#include <SDL_mixer.h> 
#include <fstream> 
#include <iostream> 
using namespace std; 


/////////////////////////////////// functions & global variable //////////////////////////////// 

SDL_Window* window; 
SDL_Renderer* renderer; 
SDL_Color textColor; 
TTF_Font* font; 
SDL_Surface* surface=SDL_GetWindowSurface(window); 
SDL_Event event; 




SDL_Texture* LoadTexture(char* path) 
{ 
    SDL_Surface* surface = IMG_Load(path); 
    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer,surface); 
    SDL_FreeSurface(surface); 
    return texture; 
} 



///////////////////////////////////// Cell ///////////////////////////////////// 

class Cell 
{ 
private: 
    char num; 

public: 
    Cell(); 
    Cell(int ,int ,char); 
    char * img; 
    pair <int ,int> cor; 
    int flag; 
    int size; 
    void display(); 
    bool indomain(int ,int); 
}; 


Cell :: Cell(int x ,int y ,char n){ 
    num=n; 
    cor.first=x; 
    cor.second=y; 
    size=40; 
    flag=0; 
    if (num == '#') 
     img="0.png"; 
    else if (num == '!') 
     img="1.png"; 
    else 
     img="street.png"; 
} 

bool Cell :: indomain(int x ,int y){ 
    if ((cor.first<=x && x<=cor.first+size) && (cor.second<=y && y<=cor.second+size)) 
     return true; 
    return false; 
} 


void Cell :: display(){ 
    SDL_Rect rect; 
    rect.x=cor.first; 
    rect.y=cor.second; 
    SDL_Texture* texture = LoadTexture(img); 
    SDL_RenderCopy(renderer,texture,NULL,&rect); 
} 


//////////////////////////////////////////// Tower ///////////////////////////////////////// 

class Tower 
{ 
public: 
    Tower(); 
    Tower(int ,int ,int ,int ,char* ,int ,int ,int); 
    ~Tower(); 
    int price; 
    int up_cost; 
    char * img; 
    int power; 
    int domain; 
    int grade; 
    int index; 
    pair <int ,int> cor; 
    void upgrade(); 
    void display(); 
    bool mark(int ,int); 
}; 


Tower :: Tower(int pr ,int co ,int pow ,int dom ,char* im ,int x ,int y ,int i){ 
    price=pr; 
    up_cost=co; 
    power=pow; 
    domain=dom; 
    img=im; 
    cor.first=x; 
    cor.second=y; 
    grade=1; 
    index=i; 
} 

void Tower :: upgrade(){ 
    if (grade<5) 
    { 
     grade+=1; 
     power*=2; 
     domain*=1.2; 
     price*=1.2; 
    } 
} 

void Tower :: display(){ 
    SDL_Rect rect; 
    rect.x=cor.first; 
    rect.y=cor.second; 
    SDL_Texture* texture = LoadTexture(img); 
    SDL_RenderCopy(renderer,texture,NULL,&rect); 
} 

bool Tower :: mark(int x, int y){ 
    if ((cor.first<=x && x<=cor.first+40) && (cor.second<=y && x<=cor.second+80)) 
     return true; 
    return false; 
} 





//////////////////////////////// Enemy ////////////////////////////////////// 

class Enemy 
{ 
public: 
    Enemy(); 
    Enemy(int ,char* ,int ,int ,int); 
    ~Enemy(); 
    char * img; 
    int health; 
    int cellNum; 
    int index; 
    pair <int ,int> cor; 
    bool live(); 
    void display(); 
    void damge(Tower t); 
}; 


Enemy :: Enemy(int h ,char* im ,int x ,int y ,int i){ 
    health=h; 
    img=im; 
    cor.first=x; 
    cor.second=y; 
    cellNum=0; 
    index=i; 
} 

bool Enemy :: live(){ 
    if (health>0) 
     return true; 
    return false; 
} 

void Enemy :: damge(Tower t){ 
    health-=t.power; 
} 

void Enemy :: display(){ 
    SDL_Rect rect; 
    rect.x=cor.first; 
rect.y=cor.second; 
SDL_Texture* texture = LoadTexture(img); 
SDL_RenderCopy(renderer,texture,NULL,&rect); 
} 




///////////////////////////////////////////// Map /////////////////////////////////////// 

class Map 
{ 
private: 
char * file; 
int len; 

public: 
Map(); 
Map(char* ,int); 
~Map(); 
Cell map[15][20]; 
pair <int ,int> * path; 
void display(); 
void put_tower(Tower ,Cell); 
void put_enemy(Enemy ,Cell); 
void remove(Cell); 
void enemyMove(Enemy); 
void pathFinder(int); 
}; 


Map :: Map(char * f ,int l){ 
file=f; 
len=l; 
ifstream infile; 
infile.open(file); 
char * line=new char; 
for (int i=0;i<15;i++) 
{ 
    infile >> line; 
    for (int j=0;j<20;j++) 
     map[i][j]=Cell(j*40 ,i*40 ,line[j]); 
} 
this->pathFinder(len); 
} 

void Map :: put_enemy(Enemy e ,Cell c){ 
e.cor=c.cor; 
e.display(); 
} 

void Map :: put_tower(Tower t ,Cell c){ 
t.cor=c.cor; 
t.display(); 
} 

void Map :: remove(Cell c){ 
c.display(); 
} 

void Map :: enemyMove(Enemy e){ 
e.cellNum+=1; 
e.cor=path[e.cellNum]; 
} 

void Map :: pathFinder(int l){ 
path=new pair<int ,int> [l]; 
ifstream infile; 
infile.open(file); 
char ** lines=new char* [15]; 
for (int i=0;i<15;i++) 
    infile >> lines[i]; 
for (int i=0;i<20;i++) 
    if (lines[0][i]=='0') 
    { 
     path[0]=make_pair(0,i); 
     break; 
    } 
int x=0; 
int y=path[0].second; 
int ord=48; 
for (int i=1;i<l;i++) 
{ 
    if (int(lines[x-1][y])==((ord-47)%10)+48) 
     x-=1; 
    else if (int(lines[x+1][y])==((ord-47)%10)+48) 
     x+=1; 
    else if (int(lines[x][y-1])==((ord-47)%10)+48) 
     y-=1; 
    else if (int(lines[x][y+1])==((ord-47)%10)+48) 
     y+=1; 
    path[i]=make_pair(x,y); 
    ord+=1; 
} 
} 

void Map :: display(){ 
for (int i=0;i<15;i++) 
    for (int j=0;j<20;j++) 
     map[i][j].display(); 
} 





//////////////////////////////////////////////////// Game //////////////////////////////////////// 

class Game 
{ 
private: 
int health; 
int money; 
int score; 
int step; 

public: 
Game(int ,int); 
~Game(); 
bool End; 
int enemyNum; 
int towerNum; 
Enemy enemys[20]; 
Tower towers[50]; 
int getHealth(); 
int getMoney(); 
int getScore(); 
int getStep(); 
void setHealth(int); 
void setMoney(int); 
void setScore(int); 
void setStep(int); 
void distroy_enemy(Enemy ,Cell); 
void distroy_tower(Tower ,Cell); 
void shooting(Tower); 
void drag_drop(Tower); 
void upgradeTower(Tower); 
void buyTower(Tower); 
void sellTower(Tower ,Cell); 
void display(Map); 
void displayMenu(); 
void endDisplay(); 
bool checkEnd(); 
void animation(); 
}; 

Game :: Game(int h ,int m){ 
health=h; 
money=m; 
step=1; 
score=0; 
enemyNum=0; 
towerNum=0; 
End=false; 
} 

int Game :: getHealth(){ 
return health; 
} 
int Game :: getMoney(){ 
return money; 
} 

int Game :: getScore(){ 
return score; 
} 

int Game :: getStep(){ 
return step; 
} 

void Game :: setHealth(int h){ 
health=h; 
} 

void Game :: setMoney(int m){ 
money=m; 
} 

void Game :: setScore(int s){ 
score=s; 
} 

void Game :: setStep (int s){ 
step=s; 
} 

void Game :: distroy_enemy(Enemy e,Cell c){ 
for (int i=e.index;i<enemyNum-1;i++) 
    enemys[i]=enemys[i+1]; 
enemyNum-=1; 
c.display(); 
} 

void Game :: distroy_tower(Tower t ,Cell c){ 
for (int i=t.index;i<towerNum-1;i++) 
    towers[i]=towers[i+1]; 
towerNum-=1; 
c.display(); 
} 

void Game :: shooting(Tower t){ 
for(int i=0;i<enemyNum;i++) 
{ 
    if (sqrt(pow(enemys[i].cor.first,2)+pow(enemys[i].cor.second,2)) <= t.domain) 
     enemys[i].health-=t.power; 
} 
} 

void Game :: upgradeTower(Tower t){ 
t.upgrade(); 
money-=t.up_cost; 
} 

void Game :: buyTower(Tower t){ 
money-=t.price; 
towers[towerNum]=t; 
towerNum+=1; 
t.display(); 
} 

void Game :: sellTower(Tower t ,Cell c){ 
distroy_tower(t,c); 
money+=t.price/2; 
} 

void Game :: endDisplay(){ 
End=true; 
SDL_FillRect(surface,&surface->clip_rect,SDL_MapRGB(surface->format,0XFF,0XFF,0XFF)); 
SDL_Surface* text; 
font=TTF_OpenFont("AV.TTF",40); 
textColor.b=20; 
textColor.g=10; 
textColor.r=0; 
if (health<=0) 
{ 
    text=TTF_RenderText_Solid(font ,"You lose" ,textColor); 
} 
else if (health>0) 
{ 
    text=TTF_RenderText_Solid(font ,"Complete the mission" ,textColor); 
} 
SDL_Rect rect; 
rect.x=300; 
rect.y=300; 
SDL_BlitSurface(text,NULL,surface,&rect); 
} 

bool Game :: checkEnd(){ 
if (health<=0 || step==20) 
    return true; 
return false; 
}  

void Game :: displayMenu(){ 
SDL_Rect rect; 
rect.x=800; 
rect.y=0; 
SDL_Texture* texture = LoadTexture("menu.png"); 
SDL_RenderCopy(renderer,texture,NULL,&rect); 
} 


void Game :: display(Map m){ 
m.display(); 
for (int i=0;i<towerNum;i++) 
    towers[i].display(); 
for (int i=0;i<enemyNum;i++) 
    enemys[i].display(); 
this->displayMenu(); 
/////////// ttf 
} 

void Game :: animation(){ 
SDL_Rect Lrect; 
Lrect.x=83; 
Lrect.y=0; 
SDL_Rect Rrect; 
Rrect.x=915; 
Rrect.y=0; 
SDL_Texture* leftpic = LoadTexture("left.png"); 
SDL_Texture* rightpic = LoadTexture("right.png"); 
for (int i=0;i<12;i++) 
{ 
    SDL_RenderCopy(renderer,leftpic,NULL,&Lrect); 
    SDL_RenderCopy(renderer,rightpic,NULL,&Rrect); 
    Rrect.x-=20; 
    Lrect.x+=60; 
    SDL_RendererFlip(); 
    SDL_Delay(300); 
} 
} 





////////////////////////////////////////////////////////////// main ////////////////////////////////////////////////////////////////////// 


int main() 
{ 
window=SDL_CreateWindow("Frontline Defence",SDL_WINDOWPOS_UNDEFINED, 
    SDL_WINDOWPOS_UNDEFINED,940,600,SDL_WINDOW_SHOWN); 
Game game(1000,500); 
Map map("map.txt",30); 
while (!game.checkEnd()) 
    { 
     if (SDL_PollEvent(&event)) 
     { 
      if (event.type == SDL_QUIT) 
       return 0; 
      if (event.type == SDL_MOUSEBUTTONDOWN) 
      { 
       SDL_Delay(3000); 
      } 
     } 
    } 
    game.endDisplay(); 
    return 0; 
} 

我做的每一件事我可以,但什么都没有改变! 我试过,包括图书馆,输入代码的所有部分在主文件等多种方式,但它并没有帮助

这些都是错误的:

Error 12 error LNK1120: 8 unresolved externals C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\Debug\ConsoleApplication1.exe ConsoleApplication1 
Error 4 error LNK2019: unresolved external symbol "public: __thiscall Cell::Cell(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall Map::Map(char *,int)" ([email protected]@[email protected]@Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1 
Error 8 error LNK2019: unresolved external symbol "public: __thiscall Enemy::~Enemy(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall Game::Game(int,int)" ([email protected]@[email protected]@Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1 
Error 7 error LNK2019: unresolved external symbol "public: __thiscall Enemy::Enemy(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall Game::Game(int,int)" ([email protected]@[email protected]@Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1 
Error 10 error LNK2019: unresolved external symbol "public: __thiscall Game::~Game(void)" ([email protected]@[email protected]) referenced in function "int __cdecl SDL_main(void)" ([email protected]@YAHXZ) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1 
Error 9 error LNK2019: unresolved external symbol "public: __thiscall Map::~Map(void)" ([email protected]@[email protected]) referenced in function "int __cdecl SDL_main(void)" ([email protected]@YAHXZ) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1 
Error 6 error LNK2019: unresolved external symbol "public: __thiscall Tower::~Tower(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall Game::Game(int,int)" ([email protected]@[email protected]@Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1 
Error 5 error LNK2019: unresolved external symbol "public: __thiscall Tower::Tower(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall Game::Game(int,int)" ([email protected]@[email protected]@Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1 
Error 11 error LNK2019: unresolved external symbol _SDL_main referenced in function _main C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\SDL2main.lib(SDL_windows_main.obj) ConsoleApplication1 
+1

链接器告诉你,你没有这些函数的实现。例如,你需要'Cell :: Cell(){/ *东西可能会在这里}'和'Enemy ::〜Enemy(){/ *东西可能会在这里* /}'等等,我不确定'_SDL_main' - 可能是库或其他配置问题。 –

+0

我已经完成了你所说的工作。但是有一个新问题。在编译代码时,程序中断,此消息框打开“ConsoleApplication1.exe中的0x00D02601未处理的异常:0xC0000005:访问冲突写入位置0xCDCDCDCD”。我现在应该怎么做? – rkarami

回答

2

你得到链接器错误引用了构造函数和析构函数,因为你没有提供这些函数的任何实现(正如Michael Burr在评论中指出的那样)。

unresolved external symbol _SDL_main referenced in function _main是因为main功能需要有特定的签名:int main(int argc, char* argv[])并检查你也有SDL2main.lib在输入连接器。

最后,您似乎从不创建任何renderer(除非您创建的代码示例中未包含某个地方),因此在创建SDL_Window in main后,必须添加对SDL_CreateRenderer()的调用。

+0

我已经完成了你所说的工作。但是有一个新问题。在编译代码时,程序中断,此消息框打开“ConsoleApplication1.exe中的0x00D02601未处理的异常:0xC0000005:访问冲突写入位置0xCDCDCDCD”。我现在应该怎么做? – rkarami

相关问题