错误:预期的不合格ID错误:含义和解决方法? [英] error: expected unqualified-id error: Meaning and fix?

查看:515
本文介绍了错误:预期的不合格ID错误:含义和解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,它来自python3和QBASIC,并且很难读取编译器错误并理解它们,因此很难调试。

I'm just learning C++, coming out of python3 and QBASIC, and am having a very hard time reading the compiler errors and understanding them, making it difficult to debug.

我遇到的问题是我不断拉出编译错误:

The problem I am having is that I keep pulling the compilation-error:

错误:预期的不合格ID

error: expected unqualified-id

这发生在第10和18行。

This occurs on the 10th and 18th line.

我正在尝试使用linuxs的g ++编译该程序:

I am trying to compile this program using 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;
        }
    }
}

目标是允许用户输入几个数字,然后给他们选择在数字上使用指定运算符的选项。

The goal is to allow the user to enter a couple numbers and then give them the option to use specified operators on the number.

请注意,我是该语言的新手,因此它可能充满语法错误和不一致之处。但是,问题是,为什么对这两个对象(而不是其他对象)提取不合格ID,这是什么意思,以及如何解决这个问题。

Note, I am new to this language so it is probably riddled with syntax errors and inconsistencies. The question though is, why does the unqualified-id get pulled for these two, (but not the others), what does it mean, and how would one go about fixing this.

我在这里问是因为我试图独立学习,因此我也没有老师或同伴转弯。也欢迎提供有关如何以更有效的代码完成此操作的建议,并且将不胜感激。

I ask here because I'm trying to learn this independently, therefore I have no instructor or peers to turn too. Advice as to how I could have done this in a more effective code is also welcome and would be much appreciated.

谢谢。

在终端中出现的错误:

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

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


推荐答案

如@ user657267所述声明函数及其实现时的分号。如果您要

As stated by @user657267 get rid of the semicolons when declaring a function and its implementation. If you were to have

int some_function(int a, int b);

在主体上方并在主体下方执行该功能

above the main and the implementation of that function below the main

int some_function(int a, int b) {
    //something happens here
     return a;
}

没关系。该实现也可以放在主要部分之上,然后您不必编写定义函数的第一行。定义或实现必须高于主体的原因是c或c ++,否则将看不到该函数,否则也会引发错误。

That would be ok. The implementation can also go above the main and then you don't have to write the first line defining the function. The reason the definition or implementation has to be above the main is c, or c++ for that matter, won't be able to see the function otherwise which will throw and error as well.

这篇关于错误:预期的不合格ID错误:含义和解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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