C++ 错误:预期标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19323195/
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 22:42:46  来源:igfitidea点击:

Error: Expected Identifier

c++

提问by Kaptain

So right now I'm just trying to set up a simple class, and can't figure out why I'm getting en error. Here is my header file:

所以现在我只是想设置一个简单的类,但不知道为什么我会出错。这是我的头文件:

#define Mob

class Mob{
private:
    int lvl;
    float hp;
public:
    Mob(int, float); //Error expected an identifier on the float
};

and the cpp file with it

以及带有它的 cpp 文件

#include "Mob.h"

Mob::Mob(int level, float health) // Error expected an identifier on the int and float
// and an Error: Expected a ; after the )
{
    hp = health;
    lvl = level;
} 

回答by Carl Norum

This line:

这一行:

#define Mob

Causes every instance of the word Mobto be replaced with nothing in your code. Don't do that.

导致Mob在您的代码中用任何内容替换单词的每个实例。不要那样做。

It looks like you wanted to make an include guard, which should look something like:

看起来您想要制作一个包含守卫,它应该是这样的:

#ifndef MOB_H
#define MOB_H


  ...

#endif

回答by jrok

You defined Mobto... nothing. That makes your code equivalent to:

你定义Mob为……什么都没有。这使您的代码等效于:

class {
private:
    int lvl;
    float hp;
public:
    (int, float); // Expecting an identifier indeed
};

and that holds for the rest of the code where #define Mobis included.

这适用于包含的其余代码#define Mob

If you're trying to make include guards, you need a unique name and define it conditionaly:

如果您尝试制作包含守卫,您需要一个唯一的名称并有条件地定义它:

#ifndef UNIQUE_MOB
#define UNIQUE_MOB
// code
#endif

回答by Red Hat Hacker

As far as I know everything that exists in C++ directives are known as entities except Processor directives(e.g macro , symbolic constants etc) and about pointers keep it in mind that for ease of applying them , imagine them as integer variables whose duty is keeping the address of memory storage locations this is good hack in the level where you are learning about pointers for the 1st time

据我所知,C++ 指令中存在的所有东西都被称为实体,除了处理器指令(例如宏、符号常量等),关于指针请记住,为了便于应用它们,将它们想象成整数变量,其职责是保持内存存储位置的地址,这是您第一次学习指针的级别的好技巧