C ++编程错误:预期的无限定id之前“{”令牌 [英] C++ Programming Error: expected unqualified-id before "{" token

查看:1017
本文介绍了C ++编程错误:预期的无限定id之前“{”令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我试图创建一个计算器:添加两个数字,减去两个数字,乘以两个数字,除数两个数字,取数字的正弦,取余数数字,或取数字的正切。这里是代码:

  #include< iostream> ;; 
#include< cmath> ;;
#include< string>
int main()
{}
int ask(std :: string operation);
{
std :: cout<<类型加法,减法,乘法,除法,正弦,余弦或切线:\\\

std :: cin>> operation;
if(operation =Addition)
{
goto Add
}
float Add(float addend1,float addend2,float answer)
{
add:
std :: cout<<插入要添加的第一个数字:\\\
;
std :: cin>> addend1;
std :: cout<< 插入要添加的第二个数字:\\\
;
std :: cin>> addend2;
answer = addend1 + addend2;
std :: cout<< addend1<<+<< addend2<<=<< answer<<\\\

break
}
}

,但我的问题是在第7行。有一个错误说:预期未限定的id之前{令牌。我知道我的缩进是可怕的,但是谢谢!

解决方案

您的代码中有很多问题。

$首先,正如Ivan指出的,你正在尝试在一个函数内定义一个函数( ask() main())。这是无效的。



其次,你有一个 goto 标签在另一个函数。我怀疑你的编译器甚至会允许,但你怎么期望这样工作?你试图使用传递给你的函数 addition 的变量不存在,因为你永远不会调用函数,堆栈从未设置过。这是坏的,不要这样做,只是正确调用函数。



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



第四,您错误地尝试分配 const char * 添加到操作当你打算使用的是等于运算符 == 。这不会工作以太,虽然因为操作是一个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< 类型加法,减法,乘法,除法,正弦,余弦或切线:\\\
;

字符串操作;
cin>>操作;

if(operation ==Addition)
{
float first = 0,second = 0;
cout<< 输入第一个操作数;
cin>>第一;

cout<< enter second operand;
cin>>第二;

cout<< 结果是:<添加(第一,第二);
}
}

float Add(float first,float second)
{
return first + second;
}


I am a newbie at C++, and I am trying to make a "calculator" which: adds two numbers, subtracts two numbers, multiplies two numbers, divides two numbers, takes the sine of a number, takes the cosine of a number, or takes the tangent of a number. Here is the code:

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

There will be more functions later, but my problem is on line 7. There is an error that says: expected unqualified-id before "{" token. I know my indentation is horrible, but thanks!

解决方案

You have a lot of issues in your code.

First, as Ivan points out, you are trying to define a function inside of a function (ask() inside main()). That isn't valid.

Second, you have a goto (why?!) attempting to jump to a label in another function. I doubt your compiler will even allow that, but how would you expect that to work? You are attempting to use variables passed to your function addition that don't exist as you never call the function and the stack has never been setup for it. This is bad, don't do it, just call the function properly.

Third, the #include preprocessor directive is terminated with a newline, not a semicolon. That could cause some (relatively) hard to track down compilation errors.

Fourth, you are mistakenly attempting to assign the const char* "Addition" to operation when what you meant to use was the equality operator ==. That won't work ether though because operation is an r-value and cannot be assigned to like that. If you want to modify it you will need to declare it as a pointer, but once again, that's not what you are going for semantically...

If you want to compare strings and (for whatever reason...) are intent on using pointers to char then you should be using strcmp. That said, you are in C++ land, so just use std:string instead.

Try something like this. I haven't enhanced your code in anyway, just made it something that will compile and run. I have made a few changes.

Aside from getting rid of a few syntax errors, your original Add function took the result as a float argument. Assigning to that from within the function would only modify a copy. You would need to take a pointer or reference if you want the caller to see the modified value, but you don't need that at all as you can simply return the result.

The string comparison is case sensitive, so you would probably want to change it to be case insensitive. I'm assuming no localization here :). I'm not performing error checking on the input either, so be aware that it may fail if the user enters something other than a valid floating point number.

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

这篇关于C ++编程错误:预期的无限定id之前“{”令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆