我只是学习C ++,来自python3和QBASIC,并且很难阅读编译器错误并理解它们,这使得调试变得困难。
我遇到的问题是我一直在拉编译错误:
错误:预期的unqualified-id
这发生在第10和第18行。
我正在尝试使用linuxs的g ++编译这个程序:
g++ proto.cpp -o prototype
该计划的代码如下。
#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;
}
}
}
目标是允许用户输入几个数字,然后为他们提供在数字上使用指定运算符的选项。
注意,我是这种语言的新手,因此很可能会出现语法错误和不一致的情况。但问题是,为什么不合格的id会被拉到这两个,(但不是其他人),这意味着什么,以及如何解决这个问题。
我在这里问,因为我试图独立学习这一点,因此我也没有教练或同伴转过身来。关于如何在更有效的代码中完成此操作的建议也是受欢迎的,我们将不胜感激。
谢谢。
它们出现在终端中的错误:
proto.cpp:10:1: error: expected unqualified-id before ‘{’ token
{
^
proto.cpp:18:1: error: expected unqualified-id before ‘{’ token
{
^
答案 0 :(得分:2)
如@ user657267所述,在声明函数及其实现时删除分号。如果你有
int some_function(int a, int b);
主要上面和主要
下面的那个函数的实现int some_function(int a, int b) {
//something happens here
return a;
}
那没关系。实现也可以在main之上,然后您不必编写定义函数的第一行。定义或实现必须高于main的原因是c或c ++,否则将无法看到抛出和错误的函数。