我对异常类有疑问... “对 GameProject::GamePlayerNullPointerException::~GamePlayerNullPointerException 的 undefined reference ”
线上:抛出 GamePlayerNullPointerException();
游戏.h
#ifndef GAME_H
#define GAME_H
#include <iostream>
#include "Player.h"
#include "Platform.h"
#include "Item.h"
#include <exception>
#include <stdexcept>
#include <string>
#define string std::string
#define ostream std::ostream
#define istream std::istream
namespace GameProject {
class Game
{
public:
friend class Inner;
Game();
~Game();
void startNew();
void quit();
void pause();
void resume();
void update();
void moveLeft();
void moveRight();
int getScore();
protected:
private:
class Inner;
Inner *i;
Game(const Game& g);
};
class GameException : public std::exception{
private:
string error;
public:
GameException(const string& message ): error(message){};
~GameException()throw ();
virtual const char* what() throw ();/*const{
return error.c_str();
throw ();
}*/
};
class GameNullPointerException: public GameException{
public:
GameNullPointerException(const string & message)
: GameException(message ) {};
~GameNullPointerException() throw ();
};
class GamePlayerNullPointerException: public GameNullPointerException{
public:
GamePlayerNullPointerException(const string & message = "Player not exist!")
: GameNullPointerException( message )
{}
~GamePlayerNullPointerException() throw ();
};
class GamePlatformNullPointerException: public GameNullPointerException{
public:
GamePlatformNullPointerException()
: GameNullPointerException( "Platform not exist!" ){}
~GamePlatformNullPointerException() throw ();
};
class GamePlayerWrongPositionException: public GamePlayerNullPointerException{
public:
GamePlayerWrongPositionException(): GamePlayerNullPointerException( "Player off screen!!!" ){ }
~GamePlayerWrongPositionException() throw ();
};
}
#undef string
#undef ostream
#undef istream
#endif // GAME_H
游戏.cpp
void Game::startNew() {
if(i->pla==NULL)
throw GamePlayerNullPointerException();
i->pla = new Player(20,20);
i->init();
}
有什么想法吗?
最佳答案
~GamePlayerNullPointerException() throw ();
您已经声明了析构函数但没有定义它。将声明更改为 .h
文件中的定义:
~GamePlayerNullPointerException() throw () { }
或者在.cpp
文件中添加定义:
GamePlayerNullPointerException::~GamePlayerNullPointerException() throw ()
{
}
或者,如果它什么都不做,就把它去掉。如果您不提供析构函数,编译器将为您生成一个空的析构函数。
关于c++ - 未定义的异常引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960300/