C++ 函数名称之前的预期初始化程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5675208/
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
Expected initializer before function name
提问by Ion
#include <iostream>
#include <string>
using namespace std;
struct sotrudnik {
string name;
string speciality;
string razread;
int zarplata;
}
sotrudnik create(string n,string spec,string raz,int sal) {
sotrudnik temp;
temp.name=n;
temp.speciality=spec;
temp.razread=raz;
temp.zarplata=sal;
return temp;
}
*sotrudnik str_compare (string str1, string str2, sotrudnik sot1, sotrudnik sot2)
I try to learn C++. But when i try to compile this code with GCC-4.4.5 by using the options " g++ -Wall -c ", I get the following error:
我尝试学习 C++。但是,当我尝试使用选项“ g++ -Wall -c ”使用 GCC-4.4.5 编译此代码时,出现以下错误:
g++ -Wall -c "lab2.cc" (in directory: /home/ion/Univer/Cpp)
lab2.cc:11: error: expected initializer before
create
lab2.cc:20: error: expected constructor, destructor, or type conversion beforestr_compare
Compilation failed.
g++ -Wall -c "lab2.cc"(在目录:/home/ion/Univer/Cpp)
lab2.cc:11: 错误:
create
lab2.cc:20之前的预期初始值设定项:错误:str_compare
编译失败之前预期的构造函数、析构函数或类型转换。
Both errors are tied to the function declarations. (round 11 is the declaration of function create, round 20 - of the function str_compare
). Tried to google for these kinds of errors, but couldn't find examples of similar errors, as the error messages are very generic. How can I understand their meaning and how to solve them? Thank you very much for your attention.
这两个错误都与函数声明有关。(第 11 轮是函数 create 的声明,第 20 轮是函数的声明str_compare
)。试图用谷歌搜索这些类型的错误,但找不到类似错误的例子,因为错误消息非常通用。我如何理解它们的含义以及如何解决它们?非常感谢您的关注。
回答by bmargulies
You are missing a semicolon at the end of your 'struct' definition.
您在“结构”定义的末尾缺少分号。
Also,
还,
*sotrudnik
needs to be
需要是
sotrudnik*
回答by Laura Doan
Try adding a semi colon to the end of your structure:
尝试在结构末尾添加分号:
struct sotrudnik {
string name;
string speciality;
string razread;
int zarplata;
} //Semi colon here