繁体   English   中英

错误:main声明中有两个或多个数据类型?

[英]error: two or more data types in declaration of main?

编码非常新,所以请理解;)

我基本上是在尝试使用while循环和if语句制作一个计算器。

#include <iostream>

using namespace std;

int x = 1;
int number;
int total = 0;
int amt = 1;
int a;
int b;
int c;
int d;
string ans;

class OperateClass

我收到错误:“ main”的声明中有两个或多个数据类型

请解释这意味着什么/如何解决。

我还想知道是否需要为每个函数创建一个新对象(加,减,乘和除)

请帮忙!

int main()
{

    cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl;
    cin >> ans;

    if(ans == "add"){
        OperateClass opOper;
        opOper.add();

    }else{

        if(ans == "subtract"){
            OperateClass opOper
            opOper.subtract();
                }

    }else{

        if(ans == "multiply"){
            OperateClass opOper
            opOper.multiply();
        }

    }else{

        if(ans == "divide"){
            OperateClass opOper
            opOper.divide();

        }
    }

}

class OperateClass{
    public:
        int add(){
            while(x <= 3){
                cout << "Enter a number to use to add: " << endl;
                cin >> a;
                total = total + a;
                x++;
                amt++;
            }
        }

        int subtract(){
            while(x <= 3){
                cout << "Enter a number to use to add: " << endl;
                cin >> b;
                total = total - b;
                x++;
                amt++;
            }
        }

        int multiply(){
            while(x <= 3){
                cout << "Enter a number to use to add: " << endl;
                cin >> c;
                total = total * c;
                x++;
                amt++;
            }
        }

        int divide(){
            while(x <= 3){
                cout << "Enter a number to use to add: " << endl;
                cin >> d;
                total = total / d;
                x++;
                amt++;
            }
        }
}

int print(){
    cout << "Your total is: " << total << endl;

    return 0;
}

该代码均无效。 您可以从class OperateClass类开始一个类定义,但是永远不要结束它并直接进入main 一个(简化的)类定义采用以下形式:

class [name] {

};  // semi-colon terminates definition

// so...

class OperateClass {

};  

接下来,您声明main ...,但它会导致else分支(?)。

int main()
{

    cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl;
    cin >> ans;

    if(ans == "add"){
    OperateClass opOper;
    opOper.add();

}else{  // what is this?

功能还必须通过其右括号终止,即

int main() {

}  // function is over!

现在看来,这些可能只是复制/粘贴错误。 如果是这种情况,那么您可能只是忘记了类定义末尾的分号。

考虑以下程序:

class C

int main() {

}

class C {

}

void someFunc(){}

这基本上就是您的程序归结为必需品。 错误如下:

error: two or more data types in declaration of 'main'
error: expected ';' after class definition

这是更正的代码:

class C; //<--semicolon in forward declaration

int main() {

}

class C {

}; //<--semicolon after class definition

void someFunc(){}

让我们对您的代码进行一些修改,以使语法正确。

int main()
{

    cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl;
    cin >> ans;

    if(ans == "add"){
    OperateClass opOper;
    opOper.add();
        }

else if (ans == "subtract"){
    OperateClass opOper;
    opOper.subtract();
        }

else if (ans == "multiply"){
    OperateClass opOper;
    opOper.multiply();
        }

else if(ans == "divide"){
    OperateClass opOper;
    opOper.divide();

        }
else {} 

} //end of main

在每种情况下,您的类变量声明语句“ OperateClass opOper”也会重复,您也可以在if-else条件之外编写该语句,以避免重复,因为无论任何情况,该语句都是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM