gpt4 book ai didi

c++ - 错误 : expected unqualified-id error: Meaning and fix?

转载 作者:行者123 更新时间:2023-11-30 03:44:21 24 4
gpt4 key购买 nike

<分区>

我刚刚学习 C++,刚从 python3 和 QBASIC 走出来,很难阅读编译器错误并理解它们,这使得调试变得困难。

我遇到的问题是我一直在拉编译错误:

错误:需要不合格的 id

这发生在第 10 和第 18 行。

我正在尝试使用 linux 的 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;
}
}
}

目标是让用户输入几个数字,然后让他们选择对数字使用指定的运算符。

请注意,我是这门语言的新手,所以它可能充满了语法错误和不一致之处。但问题是,为什么这两个(而不是其他)的 unqualified-id 被提取,这是什么意思,以及如何解决这个问题。

我在这里问是因为我正在尝试独立学习,因此我也没有导师或同伴可以求助。也欢迎就如何使用更有效的代码完成此操作提供建议,我们将不胜感激。

谢谢。

终端中出现的错误:

proto.cpp:10:1: error: expected unqualified-id before ‘{’ token
{
^

proto.cpp:18:1: error: expected unqualified-id before ‘{’ token
{
^

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com