C 类获取错误(SDL)
C++ classes getting error (SDL)
可能的重复:
什么是未定义的参考/未解决的外部符号错误,我该如何修复?
嗨,我是上课的菜鸟,我才开始学习它们。我需要此代码的帮助,因为我不知道为什么会遇到此错误。
这是我的代码
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: %sn", 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
和最后一个玩家。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);
}
错误是
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)
代码显然没有完成,但我只想摆脱此错误
"未解决的外部"错误在您声明和尝试使用函数时会发生,但您不实现它。特别是:
1> main.obj:错误lnk2019:未解决的外部符号"公共: __ thiscall player ::〜播放器(void)"(?? 1player @@ qae@xz)在功能中引用_wmain
是因为尽管您有Player::~Player()
的声明:
class Player
{
public:
/* ... */
~Player();
从未实现此方法。
通过实现所讨论的方法来修复此错误。
编辑:此外,您的类具有静态成员变量,这些变量已声明但从未定义。您的声明在这里:
class Player
{
/* ... */
private:
static int HP;
static int playerScore;
static int playerSpeed;
static int playerJump;
static int animRate;
};
,但此声明并未定义变量。您必须定义和初始化这些静态,否则您也将得到(正在得到)未解决的外部。
通常在CPP文件中的某个地方:
int Player::playerScore = 0;
顺便说一句,您正在使用static
成员变量作为类,该类似乎多次可以让您有效。如果这样做:
Player a;
Player b;
要实例化多个Player
对象,因为将成员变量声明为static
,它们每个人都会"共享"相同的playerScore
等。在查看您的代码时,这并没有很多意义。您可能不希望这些是static
s。
相关文章:
- SDL 映像:无法打开映像,仅显示错误消息
- SDL 应用程序错误:“if”之前应为非限定 id
- SDL窗口的错误
- std::函数,带有 SDL 事件回调的 lambda 错误
- SDL 乒乓球运动错误
- C++ SDL 中的 SIGSEGV 错误
- 即使 OpenGL、SDL 和 GLSL 都没有抛出错误,也没有绘制多边形
- 我正在尝试学习使用 SDL 制作游戏。我的书教SDL1.2.3,我无法让它在Visual Studio上工作。谁能帮忙?我的错误:
- SDL的事件处理错误
- SDL 2.0-无效的呈现程序错误
- SDL 错误:使用SDL_CreateTextureFromSurface时呈现器无效
- Button类中的错误-Windows触发了断点SDL
- SDL错误LNK1120:1未解决的外部
- SDL 磁贴地图呈现错误 C++/c
- C 类获取错误(SDL)
- OpenGL SDL链接错误
- C++ SDL 初始化所有内容并获取运行时错误
- emscripten + sdl = 抛出异常:类型错误:无法设置未定义的属性'widthNative'
- Eclipse SDL 构建错误:"启动配置中指定的程序不存在"
- 未初始化就使用SDL曲面错误