【问题标题】:C++ Programming Error: expected unqualified-id before "{" tokenC++ 编程错误:“{”标记之前的预期 unqualified-id
【发布时间】:2011-06-19 19:11:59
【问题描述】:

我是 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行。有一个错误说:expected unqualified-id before "{" token。我知道我的缩进很糟糕,但是谢谢!

【问题讨论】:

  • 为什么...为什么...为什么goto?!此外,标签在另一个函数中。非常非常糟糕的主意。你用的是什么编译器?
  • if 条件下注意===
  • @Woobie:OP 也不想要==,他想要strcmp
  • 哇,这是我见过的最糟糕的代码。我看过很多 CS 101 代码。几年前我曾经是个非官方的助教。
  • @Omnifarious:每个人都从某个地方开始。

标签: c++ compiler-errors token


【解决方案1】:

你的代码有很多问题。

首先,正如 Ivan 指出的那样,您试图在函数内部定义一个函数(ask() 内部 main())。那是无效的。

其次,您有一个goto(为什么?!)试图跳转到另一个函数中的标签。我怀疑您的编译器甚至会允许这样做,但是您希望它如何工作?您正在尝试使用传递给您的函数 addition 的变量,这些变量不存在,因为您从未调用该函数并且从未为其设置堆栈。这个不好,别做,正确调用函数就好了。

第三,#include 预处理器指令以换行符结束,而不是分号。这可能会导致一些(相对)难以追踪的编译错误。

第四,当您打算使用相等运算符== 时,您错误地尝试将const char* "Addition" 分配给operation。但这不会起作用,因为operation 是一个右值,不能像这样分配。如果你想修改它,你需要将它声明为一个指针,但再一次,这不是你想要的语义......

如果您想比较字符串并且(无论出于何种原因...)打算使用指向 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;
}

【讨论】:

  • 我的代码现在看起来像这样:#include &lt;iostream&gt;; #include &lt;cmath&gt;; #include &lt;string&gt;; int main () { int ask (){ char operation [20]; std::cout&lt;&lt;"Type Addition, Subtraction, Multiplication, Division, Sine, Cosine, or Tangent:\n"; std:cin&gt;&gt;operation; if (operation="Addition"){ float addend1, float addend2, float answer std::cout&lt;&lt;"Insert the first number to be added:\n"; std::cin&gt;&gt;addend1; std::cout &lt;&lt; "Insert the second number to be added:\n"; std::cin&gt;&gt;addend2; answer=addend1+addend2; std::cout&lt;&lt;addend1&lt;&lt;"+"&lt;&lt;addend2&lt;&lt;"="&lt;&lt;answer&lt;&lt;"\n"; } } } 但现在我有:预期 } 在 en
  • 接上一条评论:输入末尾应为“}”。
  • @smilinggoomba:是的,我没有把它解析出来。我发布了一个基于您的示例。
  • @Ed S. 哇!你做了一件了不起的工作。我懒得如此彻底地分析/解释这一切。 +1
  • @Ivan:嗯,我在等我的咖啡冲泡=D
【解决方案2】:

С++ 不允许嵌套函数。您有函数main() 并试图在其中声明函数ask()。而且编译器不知道你想要什么。

【讨论】:

  • @EdS:我使用 Xcode 编译,并编辑了我的代码。但是感谢您的帮助!
  • @smilinggoomba:澄清一下。 Xcode 是 IDE(集成开发环境)的名称。当你编译你的程序时,Xcode 会调用 Apple 版本的编译器 gcc
【解决方案3】:

我对你的代码做了一些评论,也许这会让你开始:

#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";
}

【讨论】:

  • goto 甚至可能无法编译,因为标签位于不同的函数中。
  • @Ed S.:你说得对,但他/她根本不应该使用goto
  • 当然,我在回复和对问题的评论中提到了这一点。
【解决方案4】:

让我们试着把它分解..
你不应该使用 ;关于预编译器指令。

#include <iostream>;
#include <cmath>;
#include <string>;

应该是

#include <iostream>
#include <cmath>
#include <string>

.

int main () {
    int ask (){

请参阅Ivans answer 了解此内容

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)
{

不确定这里发生了什么.. 但是让我们打破 Addition 到它自己的函数

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;

希望这对您有所帮助。

【讨论】:

  • 没关系,因为; 表示空语句。它完全有效的代码。 ideone.com/yOEHz
  • 在 Visual Studio 中很重要:warning C4067: unexpected tokens following preprocessor directive - expected a newline
  • @Default: 甚至 GCC 也会发出警告。但它有效。它不是错误。即使你写;;;;;;;;;;;;,代码也是格式良好的。
  • 好的,所以我稍微扩展了答案。希望消除反对票?
  • @Nawaz:分号在 C 和 C++ 中是非法的。预处理器语言本身就是一种语言。该语言的语法在 2003 C++ 标准的第 16 章、C99 标准的第 6.10 节中进行了描述。预处理器语句以行尾字符结束,而不是分号。
【解决方案5】:

首先 int ask() 那是什么。你为什么在这里开始一个块。 其次,由于 ask(),您有两个 {s 和三个 }。 我认为 c++ 不支持匿名函数。 第三,为什么要使用goto,当你有一个函数时,只需调用该函数。 Fourh 你的附加函数应该是 void 或者删除它的最后一个参数。 另外我认为你不需要 string.h 文件,除非你使用一些相当高级的函数,char 数组应该足够你的程序了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    相关资源
    最近更新 更多