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

Buc*_*763 1 c++ if-statement declaration function while-loop

对编码非常陌生,所以请理解;)

我基本上试图使用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
Run Code Online (Sandbox Code Playgroud)

我收到错误:“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;
}
Run Code Online (Sandbox Code Playgroud)

Ed *_* S. 5

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

class [name] {

};  // semi-colon terminates definition

// so...

class OperateClass {

};  
Run Code Online (Sandbox Code Playgroud)

接下来,您声明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?
Run Code Online (Sandbox Code Playgroud)

函数还必须通过右大括号终止,即

int main() {

}  // function is over!
Run Code Online (Sandbox Code Playgroud)

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