C++ 编程错误:令牌之前"{"预期的非限定 id
C++ Programming Error: expected unqualified-id before "{" token
我是C++的新手,我正在尝试制作一个"计算器",它可以将两个数字相加、相减、相乘、除数、取正弦、取余弦或取正切。这是代码:
#include <iostream>;
#include <cmath>;
#include <string>
int main ()
{}
int ask(std::string operation);
{
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:n";
std::cin>>operation;
if (operation="Addition")
{
goto Add
}
float Add(float addend1, float addend2, float answer)
{
Add:
std::cout<<"Insert the first number to be added:n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"n";
break
}
}
稍后会有更多的函数,但我的问题在第7行。有一个错误说:在"{"token之前应该是不合格的id。我知道我的缩进很可怕,但谢谢!
您的代码中有很多问题。
首先,正如Ivan所指出的,您试图在函数内部定义一个函数(ask()
在main()
内部)。这是无效的。
其次,您有一个goto
(为什么?!)试图跳转到另一个函数中的标签。我怀疑你的编译器是否会允许这样做,但你希望它如何工作?您正试图使用传递给函数addition
的不存在的变量,因为您从未调用过该函数,也从未为其设置过堆栈。这很糟糕,不要这样做,只需正确调用该函数即可。
第三,#include
预处理器指令以换行符而非分号终止。这可能会导致一些(相对)难以追踪的编译错误。
第四,您错误地试图将const char*
"Addition"
分配给operation
,而您想要使用的是相等运算符==
。但这在以太上不起作用,因为operation
是一个r值,不能像那样赋值。如果你想修改它,你需要将它声明为一个指针,但同样,这不是你想要的语义。。。
如果您想比较字符串,并且(无论出于什么原因…)打算使用指向char的指针,那么您应该使用strcmp。也就是说,您在C++领域,所以只需使用std:string
即可。
试试这样的东西。无论如何,我并没有增强你的代码,只是让它可以编译和运行。我做了一些改动。
除了消除一些语法错误外,最初的Add
函数还将结果作为float
参数。从函数中分配给它只会修改一个副本。如果您想让调用者看到修改后的值,则需要使用指针或引用,但您根本不需要,因为您可以简单地返回结果。
字符串比较区分大小写,因此您可能希望将其更改为不区分大小写。我假设这里没有本地化:)。我也没有对输入执行错误检查,所以请注意,如果用户输入的不是有效的浮点数字,它可能会失败。
#include <iostream>
#include <string>
using namespace std;
void Ask();
float Add( float, float );
int main( size_t argc, char* argv[] )
{
Ask();
return 0;
}
void Ask()
{
cout << "Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:n";
string operation;
cin >> operation;
if( operation == "Addition" )
{
float first = 0, second = 0;
cout << "enter first operand";
cin >> first;
cout << "enter second operand";
cin >> second;
cout << "The result is: " << Add( first, second );
}
}
float Add( float first, float second )
{
return first + second;
}
С++不允许嵌套函数。您有函数main()
,并试图在其中声明函数ask()
。编译器不知道您想要什么。
我对你的代码做了一点评论,也许这会让你开始:
#include <iostream>;
#include <cmath>;
#include <string>;
int main () {
int ask (){ //you cannot nest functions in C++
char operation [20]; //why not use the string class if you include it anyway
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:n";
std:cin>>operation;
if (operation="Addition"){ //you cannot compare char-strings in C++ like that
goto Addition; //don't use goto (I don't want to say "ever", but goto is only used in extremely rare cases) make a function call instead
}
}
float addition(float addend1, float addend2, float answer) //you probably want to declare the variables inside the function
{
Addition: //don't use labels
std::cout<<"Insert the first number to be added:n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"n";
}
让我们试着分解一下
你不应该使用;预编译器指令。
#include <iostream>;
#include <cmath>;
#include <string>;
应该是
#include <iostream>
#include <cmath>
#include <string>
int main () {
int ask (){
查看Ivans对此的回答
char operation [20];
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:n";
std:cin>>operation;
if (operation="Addition"){
您可以使用std::string,这更容易处理。然后你可以写
#include <string>
...
std::cout<<"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:n";
std::string myString;
getline(cin, myString);
if (myString == "Addition"){
goto Addition;
}
}
float addition(float addend1, float addend2, float answer)
{
不确定这里发生了什么。。但让我们打破添加到它自己的功能
void Addition(){
// do addition here
}
Addition:
std::cout<<"Insert the first number to be added:n";
std::cin>>addend1;
std::cout << "Insert the second number to be added:n";
std::cin>>addend2;
answer=addend1+addend2;
std::cout<<addend1<<"+"<<addend2<<"="<<answer<<"n";
}
不要忘记,你必须定义变量
int addend1;
int addend2;
int answer;
希望这对你有帮助。
First int ask()那是什么。你为什么在这里开始一个街区。第二,您有两个{s和三个}s,这是因为ask()。我认为c++不支持匿名函数。第三,为什么要使用goto,当你有一个函数时,只需调用该函数。第四,你的加法函数应该是void,或者去掉它的最后一个参数。此外,我认为您不需要string.h文件,除非您使用一些相当高级的函数,否则char数组应该足以满足您的程序。
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 方法内部但循环仍得到预期的不合格id错误C++
- Xcode 9.4.1 中的 Apple Mach-O 链接器 (id) 错误
- 错误:令牌 { '{' 之前应存在非限定 ID
- DCMTK 了解"DIMSE 没有有效的演示上下文 ID"错误
- C++中的"expected unqualified-id"错误是什么?
- c++ for 循环中预期的非限定 id 错误
- 在"." 令牌之前应为非限定 ID(C++ 错误)
- 预期"{"错误?以及预期的未指定 ID 错误?
- 错误:预期的非限定 ID 错误:含义和修复
- 安卓-NDK-R7C "deque.h"中的非限定 ID 错误
- 预期的不合格id错误,很可能是原因
- 意外的非限定 ID 错误
- C++中类出现预期的不合格id错误
- 出现意外的非限定id错误
- iOS中出现不合格的id错误
- XSLT到HTML的转换.Qt5中的generate-id()错误
- 不合格的id错误和井字游戏板不工作
- 传递结构体数组的预期非限定id错误
- 如何执行项目范围内的唯一id /错误代码,以便在源代码中轻松找到错误的来源