C++ 缺少语法错误;前 *

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1542623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:21:24  来源:igfitidea点击:

Syntax error missing ; before *

c++visual-c++

提问by tomjen

I have a header file like so:

我有一个像这样的头文件:

#pragma once
#include "gamestate.h"
#include "ExitListener.h"

class InitialGameState : public GameState
{
public:
  InitialGameState(Ogre::Camera *cam, Ogre::SceneManager *sceneMgr, OIS::Keyboard      *keyboard, OIS::Mouse *mouse, Ogre::Root *root);
  ~InitialGameState(void);
  virtual bool update(Ogre::Real time);
  virtual void pause(void);
  virtual void start(void);
  void keyPressed(const OIS::KeyEvent &e);
  void keyReleased(const OIS::KeyEvent &e);
//private:
ExitListener *mFrameListener;
};

The problem with this is that I get the following errors from VC 8:

问题是我从 VC 8 收到以下错误:

InitialGameState.h(16) : error C2143: syntax error : missing ';' before '*'  
InitialGameState.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int  
InitialGameState.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int  

(they all refer to the last line)

(都指最后一行)

I have a class ExitListener.hwhich is why I don't get the errors

我有一堂课ExitListener.h,这就是为什么我没有收到错误

Edit: ExitListener.h:

编辑:ExitListener.h:

#pragma once
#include <Ogre.h>
#include <OIS/OIS.h>
#include <CEGUI/CEGUI.h>
#include <OgreCEGUIRenderer.h>
#include "Thing.h"
#include "InitialGameState.h"

using namespace Ogre;
class ExitListener : public FrameListener, public OIS::KeyListener, public     OIS::MouseListener
{
public:
ExitListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, Camera *cam, std::vector<Thing*> &vec): 
  mKeyboard(keyboard), r(0.09), mContinue(true), mRunningAnimation(false),
mMouse(mouse), mYaw(0), mPitch(0), things(vec), mCamera(cam), mWDown(false), mSDown(false), mADown(false),
mDDown(false)
{
  things = vec;
  mKeyboard->setEventCallback(this);
  mMouse->setEventCallback(this);
}
bool frameStarted(const FrameEvent& evt);   
bool keyPressed(const OIS::KeyEvent &e);
bool keyReleased(const OIS::KeyEvent &e);
bool mouseMoved(const OIS::MouseEvent &e);
bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id);
bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id);

void setOwner(GameState *g);

private:
  AnimationState *mSwim;
  Radian r;
  Radian mYaw;
  Radian mPitch;
  OIS::Keyboard *mKeyboard;
  OIS::Mouse *mMouse;
  Camera *mCamera;
  bool mContinue;
  bool mRunningAnimation;
  std::vector<Thing*> &things;
  bool mWDown;
  bool mADown;
  bool mDDown;
  bool mSDown;
  GameState *mOwner;
};

Edit 2:

编辑2:

It turned out that the problem could be solved by a forward declaration and then including the other header directly in my .cpp file.

事实证明,这个问题可以通过前向声明解决,然后直接在我的 .cpp 文件中包含另一个头文件。

Thanks.

谢谢。

回答by Naveen

My guess is that ExitListener.h is including InitialGameState.h header file either directly or indirectly. So there is a circular dependency between the header file and compiler is unable to find the declaration for ExitListener. If you just need to store the pointer of ExitListener in this class then there is no need to include the ExitListener.h header file. Instead you can just use the forward declaration as class ExitListener;

我的猜测是 ExitListener.h 直接或间接包含 InitialGameState.h 头文件。所以头文件之间存在循环依赖,编译器无法找到ExitListener的声明。如果您只需要在此类中存储 ExitListener 的指针,则无需包含 ExitListener.h 头文件。相反,您可以使用前向声明作为class ExitListener;

EDIT: You can use the forward declaration as suggested above, or remove the InitialGameState.h include from ExitListener.h . You need to include GameState.h (the base class header file) only. But I prefer to use the forward declarations in header file and include the header file only in cpp.

编辑:您可以使用上面建议的前向声明,或从 ExitListener.h 中删除 InitialGameState.h 包含。您只需要包含 GameState.h(基类头文件)。但我更喜欢在头文件中使用前向声明,并且只在 cpp 中包含头文件。

回答by sbi

  1. The errors don't refer to the last line, but to the line beforethe last line. (Please be precise. If people know the compiler well which emits this error message, their guesses might be a lot better if they know the exact line it is given for.)
  2. "ExitListener.h" is not a class, but a header. (This isn't Java.) One would assume that there is a class ExitListenerdefined (or at least declared) inside that header, but there could just as well be some other class, none at all, or many classes.
  3. Without this header, it's impossible to say exactly what's wrong, although either circular dependencies between these two headers or a missing ;at the end of the ExitListenerclass' definition is a very good guess that fits my experience with such errors. At the very least I'm sure the error means that the compiler doesn't know what ExitListeneris.
  4. As others have said, you do not need a class definition in order to declare a pointer to that class, so (assuming that "ExitListener.h" defines the ExitListenerclass) you don't need to include the header at all. A simple forward declaration class ExitListener;is sufficient enough to declare the ExitListener *mFrameListenermember. (You will need to include the full class definition in order to implement the InitialGameStatemember functions that deal with ExitListener, though. If you implement these functions in the header where InitialGameStateis defined, you will need to keep that "ExitListener.h" include.)
  1. 错误不是指最后一行,而是指最后一行之前的行。(请准确。如果人们很了解发出此错误消息的编译器,如果他们知道给出的确切行,他们的猜测可能会好得多。)
  2. “ExitListener.h”不是一个类,而是一个标题。(这不是 Java。)人们会假设ExitListener在该头文件中定义了(或至少声明了)一个类,但也可能有其他一些类,根本没有,或许多类。
  3. 没有这个头文件,就不可能确切地说出哪里出了问题,尽管这两个头文件之间的循环依赖关系或类定义;末尾的缺失ExitListener都是一个很好的猜测,符合我对此类错误的经验。至少我确定错误意味着编译器不知道是什么ExitListener
  4. 正如其他人所说,您不需要类定义来声明指向该类的指针,因此(假设“ExitListener.h”定义了ExitListener该类)您根本不需要包含标题。一个简单的前向声明class ExitListener;就足以声明ExitListener *mFrameListener成员。(不过,您需要包含完整的类定义,以实现InitialGameState处理的成员函数ExitListener。如果您在InitialGameState定义的头文件中实现这些函数,则需要保留“ExitListener.h”包含。)

回答by Ashalynd

Apparently the problem is with ExitListener definition, it's not considered valid at that point.

显然问题出在 ExitListener 定义上,当时它不被认为是有效的。

回答by Patrice Bernassola

The error is in the ExitListener.h file (or any of the file it includes). Often this problem is due to a missing ;at the end of the class.

错误在 ExitListener.h 文件(或它包含的任何文件)中。通常这个问题是由于;在课程结束时遗漏了。

If you add the code of this file I will be able to help you further.

如果您添加此文件的代码,我将能够进一步帮助您。

回答by NewbiZ

The problemExitListener was incorrectly declared. This is the only solution for VS to say this. Check that there was no error when compiling the ExitListener class. (and that you did not forget the trailing ";")

问题ExitListener 被错误地声明。这是 VS 唯一的解决方案。检查在编译 ExitListener 类时没有错误。(并且您没有忘记尾随的“;”)

A side noteHere you are using a pointer to ExitListener. You do not need to know the size or internal layout of ExitListener if you just declare a pointer. A forward declaration would be enough.

旁注这里您使用的是指向 ExitListener 的指针。如果您只是声明一个指针,则不需要知道 ExitListener 的大小或内部布局。提前声明就足够了。

回答by LiraNuna

I suspect you are missing the Ogre includes somewhere on your include chain.

我怀疑您在包含链的某处缺少 Ogre 包含。

My assumption is based on the little knowledge I have about your other header files and VC alerting that it's missing a type specifier: missing type specifier.

我的假设是基于我对您的其他头文件和 VC 警告它缺少类型说明符的知之甚少:missing type specifier.