c++ - 未定义的异常引用

标签 c++

我对异常类有疑问... “对 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/

相关文章:

c++ - 强制执行某种类型的可变参数模板

c++ - 取消引用空指针

c++ - 从背景中分割前景

c++ - 你推荐什么 GNU make 替代品?

c++ - map.find(var) 没有返回正确的值

c++ - 此声明在 C++ 中没有存储类或类型说明符

c++ - 如何在 C++ 中创建 scoped_ptr 映射

c++ - 在 C++ 中,是否可以根据这些对象的任何属性轻松地对对象类型指针的 vector 进行排序?

c++ - 我可以扔流吗?

c++ - 在多大程度上可以将 C++ 指针视为内存地址?