c++ - 错误 : two or more data types in declaration of main?

标签 c++ function if-statement while-loop declaration

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

我主要是尝试使用 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!

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

关于c++ - 错误 : two or more data types in declaration of main?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11515064/

相关文章:

c++ - 在 C++ 中,从列表中删除一个对象

c++ - 虚拟方法 VS std::function 成员变量在性能方面

c++ - 如何制作包含 "Private"?

java - Mongo/Java Map-Reduce 函数 - MongoCommandException

function - 匿名函数简写

date - 如果 Get-Date 位于日期范围之间

JavaScript : loop in conditional based on array

c++ - C++中的结构和函数

C:函数返回值是否比 void 占用更多的 CPU 周期?

java - 我的代码抛出 ArrayIndexOutOfBoundsException