简体   繁体   English

错误 C2143:缺少“;” 前 '*'

[英]error C2143 : missing ';' before '*'

hello I have searched everywhere on the internet for an answer but i can't find any.您好,我在互联网上到处搜索答案,但我找不到任何答案。

code:代码:

#ifndef GAME_H
#define GAME_H

#include "drawEngine.h"
#include "sprite.h"
#include <iostream>
using namespace std;

class Game
{
public:
    bool run(void);

protected:
    bool getinput(char *c);
    void timerUpdate(void);

private:
    Sprite* player; // this gives me C2143

    double frameCount;
    double startTime;
    double lastTime;

    int posx;
    //int posy;
    DrawEngine drawArea;
};

#endif

How do I fix this?我该如何解决?

sprite.h精灵.h

#ifndef GAME_H
#define GAME_H
#include "drawEngine.h"
#include "game.h"

enum
{
    SPRITE_CLASSID,
};
struct vector
{
    float x;
    float y;

};

class Sprite
{
public:


    Sprite(DrawEngine *de, int s_index, float x = 1, float y = 1, int i_lives = 1);
    ~Sprite();
    vector getPosition(void);

    float getX(void);
    float getY(void);

    virtual void addLives(int num = 1);
    int getLives(void);
    bool isAlive(void);

    virtual bool move(float x, float y);

protected:

    DrawEngine *drawArea;
    vector pos;
    int spriteIndex;
    int numLives;
    int classID;
    vector facingDirection;
    void draw(float x, float y);
    void erase(float x, float y);

private:
};

#endif

The problem in this case appears to be that Sprite is not recognized as a type.这种情况下的问题似乎是Sprite未被识别为一种类型。 After a better look, the problem you have is that you define:仔细观察后,您遇到的问题是您定义:

#ifndef GAME_H
#define GAME_H
//...
#endif

in both files.在这两个文件中。 You do that in the.cpp file(or Game.h file.. first code snippet) and you also do it in the Sprite.h file.您在 .cpp 文件(或 Game.h 文件..第一个代码片段)中执行此操作,您也在 Sprite.h 文件中执行此操作。 The problem is that at the time that the compiler goes to Sprite.h GAME_H is already defined and therefore, thanks to the #ifndef routine it no longer compiles the Sprite.h file.问题是在编译器转到 Sprite.h 时,GAME_H 已经定义,因此,由于#ifndef例程,它不再编译 Sprite.h 文件。

To fix it change in the Sprite.h file like so:要修复它,请在Sprite.h文件中更改如下:

#ifndef SPRITE_H
#define SPRITE_H
//...
#endif

I'm guessing that this is from the compile of Sprite.cpp.我猜这是来自 Sprite.cpp 的编译。

Sprite.cpp includes sprite.h, which includes game.h at the top. Sprite.cpp 包含 sprite.h,其中包含顶部的 game.h。 The latter include includes sprite.h again, which does nothing due to its inclusion guard or pragma once.后者 include 再次包含 sprite.h,由于它的包含保护或 pragma 一次,它什么也不做。 That means, that at that point there is no known class called sprite - as in this compilation, it's below it.这意味着,那时没有已知的 class 称为 sprite - 在此编译中,它在它的下方。

Resulting code (after preprocessing, before compiling) would look like:生成的代码(预处理后,编译前)如下所示:

class Game { Sprite *... };

class Sprite { ... };

Sprite::func() {};

In essence, you can't fix this easily.从本质上讲,你不能轻易解决这个问题。 You would need to make one of the headers not depend on the other being included first.您需要使其中一个标题不依赖于首先包含的另一个标题。 You can do that by, every time you don't need the contents of the class, to forward declare it instead of including it.您可以这样做,每次您不需要class 的内容时,转发声明它而不是包含它。

class Game;
class Sprite {...};

and

class Sprite;
class Game { Sprite *...};

so if you do this and then compile sprite.cpp, the preprocessed output is going to look like所以如果你这样做然后编译 sprite.cpp,预处理的 output 看起来像

class Sprite;
class Game { Sprite *... };
class Sprite { ... };
Sprite::func() {};

which will work.这将起作用。 The compiler doesn't need to know what Sprite is exactly at the time you declare a pointer to it.在您声明指向 Sprite 的指针时,编译器不需要知道它到底是什么。 In fact, the only times you do need the full declaration is when:事实上,您唯一需要完整声明的情况是:

  • You use members of the class您使用 class 的成员
  • You inherit from the class您从 class 继承
  • You use sizeof on the class您在 class 上使用 sizeof
  • You instantiate a template with it你用它实例化一个模板

And that's about it.就是这样。 There may be more but they won't be common cases and you shouldn't run into them that quickly.可能还有更多,但它们不会是常见的情况,你不应该那么快遇到它们。 In any case, use a forward declaration first and if that really doesn't work, then include the header.在任何情况下,首先使用前向声明,如果这确实不起作用,则包括 header。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型C2143错误 - 在&#39;&lt;&#39;之前缺少&#39;,&#39; - Error of type C2143 - Missing ',' before '<' C2143:语法错误:缺少&#39;;&#39; 在“ *”之前 - C2143: syntax error : missing ';' before '*' 错误C2143:缺少语法错误:缺少&#39;;&#39; 在“ *”之前 - error C2143: missing syntax error: missing ';' before '*' 错误C2143和错误C2059在“ {”之前缺少“;” - error C2143 and error C2059 missing “;” before “{” C ++:错误C2143:语法错误:缺少&#39;;&#39; 在“ &lt;”之前 - C++: error C2143: syntax error : missing ';' before '<' 错误C2143:语法错误:缺少&#39;;&#39; 在``template &lt;&#39;&#39;之前 - error C2143: syntax error : missing ';' before ''template<'' 错误C2143:语法错误:缺少';'在'__stdcall'之前 - error C2143: syntax error : missing ';' before '__stdcall" 错误 C2143:语法错误:缺少“;” 在“如果”之前 - error C2143: syntax error : missing ';' before 'if' 错误c2143缺少&#39;;&#39; &#39;&gt;&gt;&#39;之前,错误c2143缺少&#39;;&#39; 在Visual Studio 2005中C和C ++中的cin和cout之前为&#39;&lt;&lt;&#39; - error c2143 missing ';' before '>>' and error c2143 missing ';' before '<<' for cin and cout in c++ in visual studio 2005 错误C2143:语法错误:在“:”之前缺少“,” - error C2143: syntax error : missing ',' before ':'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM