【问题标题】:error: two or more data types in declaration of main?错误:主要声明中有两种或多种数据类型?
【发布时间】:2012-07-15 22:27:14
【问题描述】:

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

我基本上是在尝试使用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;
}

【问题讨论】:

  • 你应该使用 if-else if-else 或 switch。你现在正在做的事情令人困惑。很难找到打开和关闭括号。
  • 那些遍地的全局变量真的不是个好主意。
  • 在 "if(ans == "subtract"/"multiply"/"divide")" 你忘记了 ";"在“操作类 opOper”之后
  • 好的,我不再收到分号错误
  • 编写if / else if / 序列的常用方法是将“else if”视为一个单词。我无法在评论中正确格式化它,但this gist 显示了更传统的方式来做到这一点。 (有些语言甚至有 elsifelseif 作为关键字;C++ 没有。)

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


【解决方案1】:

该代码均无效。您以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!

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

【讨论】:

  • 有一个隐藏的if。不过,它就在那里。
  • @chris:是的,这就是我添加最后一点的原因。我认为缺少分号可能是问题所在,这只是一个糟糕的复制/粘贴工作。
  • 好的。我将编辑原始代码并添加分号。
  • @Bucky763:请不要那样做。更正代码后,这个问题不再有意义。我会为你回滚编辑。
  • 嗯,我想现在已经确定问题出在分号上,分号就在这里。为你点赞。
【解决方案2】:

考虑以下程序:

class C

int main() {

}

class C {

}

void someFunc(){}

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

错误:“main”声明中有两种或多种数据类型 错误:预期的';'类定义后

这是更正后的代码:

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

int main() {

}

class C {

}; //<--semicolon after class definition

void someFunc(){}

【讨论】:

  • 现在我收到 错误:聚合 'OperateClass opOper' 类型不完整,无法定义
  • 干掉类OperateClass;并将整个班级搬到那里。
  • 在 main 让程序运行之前移动类 def!
  • 唯一的是,它不会运行我添加的这个功能void print(){ cout &lt;&lt; "Your total is: " &lt;&lt; total &lt;&lt; endl; }
  • @Bucky763,一个新问题完全更适合进一步的问题,但作为一个随机猜测,你有没有调用它?
【解决方案3】:

让我们稍微修改一下你的代码,让你的语法正确。

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 条件之外编写该语句以避免重复,因为该语句在任何情况下都是正确的。

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多