C++ 错误:预期的不合格 ID 错误:含义和修复?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35535859/
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
error: expected unqualified-id error: Meaning and fix?
提问by Careful Now
I'm just learning C++, coming out of python3 and QBASIC, and am having a very hard time reading the compiler errors and understanding them, making it difficult to debug.
我只是在学习 C++,从 python3 和 QBASIC 出来,我很难阅读编译器错误并理解它们,这使得调试变得困难。
The problem I am having is that I keep pulling the compilation-error:
我遇到的问题是我一直在拉编译错误:
error: expected unqualified-id
错误:预期的不合格 ID
This occurs on the 10th and 18th line.
这发生在第 10 行和第 18 行。
I am trying to compile this program using linuxs' g++:
我正在尝试使用 linuxs 的 g++ 编译这个程序:
g++ proto.cpp -o prototype
The code for the program is below.
该程序的代码如下。
#include <iostream>
#include <string>
using namespace std;
//Declaring Functions
//Trouble Function
int mult ( double x, double y );
{
return x * y;
}
//Trouble Function
int dive ( double x, double y );
{
if ( y == 0 )
{
cout<<"Error, cannot divide by zero.\n";
return;
}
else
{
return x / y;
}
}
//This error doesn't occur beyond this point.
int plus ( double x, double y );
{
return x + y;
}
int min ( double x, double y );
{
return x - y;
}
//End of global declarations.
//I would have made them local functions if not
//for an entirely set of unrelated problems.
int main()
{
cout<<"Please enter two numbers.\n"<<"\n";
int num1;
int num2;
cin>>num1;
cin>>num2;
string returnz = "<unknown>";
while ( returnz != "no" )
{
cout<<"What would you like to do with the numbers>\n";
cout<<'\n'<<"Enter ( mult ) to multiply, ( min ) to subtract, ( plus ) to add, and ( dive ) to divide.\n";
getline( cin, returnz, '\n' );
if ( returnz == "mult" )
{
double result = mult ( num1, num2 );
cout<<num1<<" * "<<num2<<" = "<<result<<"\n";
continue;
}
else if ( returnz == "dive" )
{
double rest = dive ( num1, num2 );
cout<<num1<<" / "<<num2<<" = "<<rest<<"\n";
continue;
}
else if ( returnz == "plus" )
{
double res = plus ( num1, num2 );
cout<<num1<<" + "<<num2<<" = "<<res<<"\n";
continue;
}
else if ( returnz == "min" )
{
double re = min ( num1, num2 );
cout<<num1<<" - "<<num2<<" = "<<re<<"\n";
continue;
}
else
{
break;
}
}
}
The goal is to allow the user to enter a couple numbers and then give them the option to use specified operators on the number.
目标是允许用户输入几个数字,然后让他们选择在数字上使用指定的运算符。
Note, I am new to this language so it is probably riddled with syntax errors and inconsistencies. The question though is, why does the unqualified-id get pulled for these two, (but not the others), what does it mean, and how would one go about fixing this.
请注意,我是这种语言的新手,所以它可能充满了语法错误和不一致之处。但问题是,为什么会为这两个(而不是其他)提取不合格的 id,这是什么意思,以及如何解决这个问题。
I ask here because I'm trying to learn this independently, therefore I have no instructor or peers to turn too. Advice as to how I could have done this in a more effective code is also welcome and would be much appreciated.
我在这里问是因为我试图独立学习这个,因此我也没有导师或同龄人可以求助。关于如何以更有效的代码完成此操作的建议也是受欢迎的,将不胜感激。
Thank you.
谢谢你。
Errors as they appear in terminal:
出现在终端中的错误:
proto.cpp:10:1: error: expected unqualified-id before ‘{' token
{
^
proto.cpp:18:1: error: expected unqualified-id before ‘{' token
{
^
回答by Careful Now
As stated by @user657267 get rid of the semicolons when declaring a function and its implementation. If you were to have
正如@user657267 所述,在声明函数及其实现时去掉分号。如果你有
int some_function(int a, int b);
above the main and the implementation of that function below the main
在 main 之上,在 main 之下执行那个函数
int some_function(int a, int b) {
//something happens here
return a;
}
That would be ok. The implementation can also go above the main and then you don't have to write the first line defining the function. The reason the definition or implementation has to be above the main is c, or c++ for that matter, won't be able to see the function otherwise which will throw and error as well.
那没问题。该实现也可以在 main 之上,然后您不必编写定义函数的第一行。定义或实现必须高于 main 的原因是 c 或 c++,否则将无法看到函数,否则它也会抛出和错误。