C++ 编译“错误:预期的构造函数、析构函数或类型转换在‘=’标记之前”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3356854/
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
c++ compiling "error: expected constructor, destructor, or type conversion before '=' token "
提问by lukmac
Very simple codes located in the same file 'foo.h':
位于同一文件 'foo.h' 中的非常简单的代码:
class Xface
{
public:
uint32_t m_tick;
Xface(uint32_t tk)
{
m_tick=tk;
}
}
std::map<uint32_t, Xface*> m;
Xface* tmp;
tmp = new Xface(100); **//Error**
m[1] = tmp; **//Error**
tmp = new Xface(200); **//Error**
m[2] = tmp; **//Error**
The error is error: expected constructor, destructor, or type conversion before '=' tokenfor every assignment.
错误是 错误:每次赋值的“=”标记之前的预期构造函数、析构函数或类型转换。
回答by Randolpho
C++ is not a scripting language. You can declare items outside the bounds of an executable block of code, but you cannot do any processing. Try moving the erroring code into a function like this:
C++ 不是脚本语言。您可以在可执行代码块的范围之外声明项目,但不能进行任何处理。尝试将错误代码移动到这样的函数中:
int main()
{
std::map<uint32_t, Xface*> m;
Xface* tmp;
tmp = new Xface(100); **//Error**
m[1] = tmp; **//Error**
tmp = new Xface(200); **//Error**
m[2] = tmp; **//Error**
}
回答by gruszczy
Your code must be inside some function, you can't just put it in void :-) Try running the same code in main and see, what happens.
你的代码必须在某个函数内,你不能把它放在 void 中 :-) 尝试在 main 中运行相同的代码,看看会发生什么。
回答by gruszczy
class Xface
{
public:
uint32_t m_tick;
Xface(uint32_t tk)
{
m_tick=tk;
}
} // need a semicolon here
You are missing a semicolon at the end of the class definition.
您在类定义的末尾缺少分号。
回答by David Thornley
You have no default constructor. You need to have a constructor that doesn't need any arguments. Right now, you've got a constructor that needs a uint32_t
, so you can't new
an array of them. Not to mention, as Neil pointed out, the missing semicolon, and gruszczy's observation that executable code needs to be in a function.
您没有默认构造函数。您需要有一个不需要任何参数的构造函数。现在,您有一个需要 a 的构造函数uint32_t
,因此您不能new
使用它们的数组。更不用说,正如 Neil 指出的那样,缺少分号,以及 gruszczy 的观察,即可执行代码需要在函数中。