C++ GCC - “')' 标记之前的预期不合格 ID”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/106117/
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
GCC - "expected unqualified-id before ')' token"
提问by epochwolf
Please bear with me, I'm just learning C++.
请耐心等待,我只是在学习 C++。
I'm trying to write my header file (for class) and I'm running into an odd error.
我正在尝试编写我的头文件(用于类),但遇到了一个奇怪的错误。
cards.h:21: error: expected unqualified-id before ')' token
cards.h:22: error: expected `)' before "str"
cards.h:23: error: expected `)' before "r"
What does "expected unqualified-id before ')' token" mean? And what am I doing wrong?
“')' 标记之前的预期不合格 ID”是什么意思?我做错了什么?
Edit: Sorry, I didn't post the entire code.
编辑:对不起,我没有发布整个代码。
/*
Card header file
[Author]
*/
// NOTE: Lanugage Docs here http://www.cplusplus.com/doc/tutorial/
#define Card
#define Hand
#define AppError
#include <string>
using namespace std;
// TODO: Docs here
class Card { // line 17
public:
enum Suit {Club, Diamond, Spade, Heart};
enum Rank {Two, Three, Four, Five, Six, Seven, Eight, Nine,
Ten, Hyman, Queen, King, Ace};
Card(); // line 22
Card(string str);
Card(Rank r, Suit s);
Edit: I'm just trying to compile the header file by itself using "g++ file.h".
编辑:我只是尝试使用“g++ file.h”自行编译头文件。
Edit: Closed question. My code is working now. Thanks everyone! Edit: Reopened question after reading Etiquette: Closing your posts
回答by davenpcj
Your issue is your #define
. You did #define Card
, so now everywhere Card
is seen as a token, it will be replaced.
你的问题是你的#define
. 你做到了#define Card
,所以现在到处都Card
被视为令牌,它将被替换。
Usually a #define Token
with no additional token, as in #define Token Replace
will use the value 1
.
通常 a#define Token
没有额外的标记,因为 in#define Token Replace
将使用 value 1
。
Remove the #define Card
, it's making line 22 read: 1();
or ();
, which is causing the complaint.
删除#define Card
,它使第 22 行变为:1();
or ();
,这导致了投诉。
回答by John Millikin
(edited for updated question)
(针对更新的问题进行了编辑)
Remove the #define
statements, they're mangling the file. Were you trying to implement an include guard? That would be something like this:
删除#define
语句,它们正在修改文件。您是否正在尝试实施包含保护?那将是这样的:
#ifndef CARD_H
#define CARD_H
class Card ...
...
#endif
old answer:
旧答案:
It means that string
is not defined in the current line. Try std::string
.
这意味着string
当前行中未定义。试试std::string
。
回答by paercebal
Just my two cents, but I guess you used the pre-compiled header
只是我的两美分,但我猜你使用了预编译的头文件
#define Card
#define Hand
#define AppError
as if you wanted to tell the compiler "Hey, the classes Card, Hand and AppError are defined elsewhere" (i.e. forward-declarations).
好像你想告诉编译器“嘿,类 Card、Hand 和 AppError 是在别处定义的”(即前向声明)。
Even if we ignore the fact macros are a pain for the exact reasons your code did not compile (as John Millikin put it, mangling your file), perhaps what you wanted to write was something like:
即使我们忽略宏是一个痛苦的事实,因为你的代码没有编译的确切原因(正如约翰米利金所说,修改你的文件),也许你想写的是这样的:
class Card ;
class Hand ;
class AppError ;
Which are forward-declarationsof those classes.
哪些是这些类的前向声明。
回答by Jorge Ferreira
Remove the #define Card.
删除#define Card。