0

我对异常类有疑问...“对 GameProject::GamePlayerNullPointerException::~GamePlayerNullPointerException 的未定义引用”

上线:throw 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();
}

有任何想法吗?

4

1 回答 1

2
~GamePlayerNullPointerException() throw ();

您已经声明了析构函数但没有定义它。将声明更改为.h文件中的定义:

~GamePlayerNullPointerException() throw () { }

或者在.cpp文件中添加定义:

GamePlayerNullPointerException::~GamePlayerNullPointerException() throw ()
{
}

或者,如果它不做任何事情,就摆脱它。如果您不提供,编译器将为您生成一个空的析构函数。

于 2012-12-19T20:08:53.763 回答