c++ - 错误 : expected constructor, 析构函数,或 '*' token 之前的类型转换

标签 c++ singleton

我有一个 C++ 类,尽管我有另一个用类似语法编写的类,可以毫不费力地编译,但我一直收到此错误。

这是我的.h:

#ifndef FISHPLAYER_H
#define FISHPLAYER_H

#include "Player.h"


class FishPlayer : public Player
{
public:
    float xThrust;
    float yThrust;
    static FishPlayer* getInstance();
protected:
private:
    FishPlayer();
    ~FishPlayer();
    static FishPlayer* instance;
};

#endif

这是我的.cpp:

#include "..\include\FishPlayer.h"

FishPlayer* FishPlayer::instance=0;  // <== I Get The Error Here

FishPlayer::FishPlayer()
{
    //ctor
    xThrust = 15.0f;
    yThrust = 6.0f;
}

FishPlayer::~FishPlayer()
{
    //dtor
}


FishPlayer* FishPlayer::getInstance() { // <== I Get The Error Here
    if(!instance) {
        instance = new FishPlayer();
    }
    return instance;
}

我已经找了一段时间了,它一定很大,我没看到。

这里是继承:

#ifndef PLAYER_H
#define PLAYER_H
#include "Ennemy.h"

class Player : public Ennemy
{
    public:
    protected:
        Player();
        ~Player();
    private:
};

#endif // PLAYER_H

更高的:

#ifndef ENNEMY_H
#define ENNEMY_H

#include "Doodad.h"

class Ennemy : public Doodad
{
    public:
        float speedX;
        float maxSpeedX;
        float speedY;
        float maxSpeedY;
        float accelerationX;
        float accelerationY;
        Ennemy();
        ~Ennemy();
    protected:
    private:
};

和父类(super class)

#include <vector>
#include <string>


enum DoodadType{FishPlayer,Player,AstroPlayer,Ennemy,DoodadT = 999};
enum DoodadRange{Close, Medium , Far};
enum EvolutionStage{Tiny, Small, Average, Large};

class Doodad
{
    public:
        float score;
        void die();
        EvolutionStage evolutionStage;
        DoodadRange range;
        Doodad();
        virtual ~Doodad();
        Doodad(Doodad const& source);
        std::vector<Animation> animations;
        double posX;
        double posY;
        std::string name;
        std::string currentAnimation;
        int currentFrame;
        DoodadType type();
        SDL_Surface getSpriteSheet();
        bool moving;
        void update();
    protected:
    private:
        SDL_Surface spriteSheet;
};

最佳答案

看起来您在 Doodad.h 中使用 FishPlayer 作为枚举值

它与您要声明的类同名。这可能是问题的根源。

一般来说,最好使用 FISH_PLAYERType_FishPlayer 之类的枚举值命名方案来避免此类冲突。

关于c++ - 错误 : expected constructor, 析构函数,或 '*' token 之前的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13922038/

相关文章:

ios - 设置属性导致 EXC_BAD_ACCESS 崩溃(CLLocationManager Singleton)

java - 构建 Android 库时,我可以使用什么模式来支持 Activity 中配置的回调

c++ - 如何在不使 Weak_ptr 的情况下解决 Shared_ptr Cyclic?

指向类内部成员函数的 C++ 函数指针

c++ - 一行类定义?

objective-c - 为什么从sharedInstance返回id

java - 为什么主线程在这里没有被抢占?

c++ - 线程通信的 myDesign 是否可以接受?

c++ - QTimeEdit 当分钟增加时如何增加到下一小时?

java - 使用泛型和反射的单例工厂