2012-07-17 144 views
1

很新的編碼,所以請理解;)錯誤:主聲明中有兩個或多個數據類型?

我基本上是試圖使用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 

我得到的錯誤:兩個或兩個以上的數據類型的「主」

請解釋這是什麼意思/如何解決它的聲明。

我也想知道,如果我需要一個新的對象爲每個功能(加,減,乘,除)

請幫幫忙!

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; 
} 
+0

你應該使用if-else if-else或switch。你現在在做什麼是令人困惑的。很難找到打開和關閉的大括號。 – 2012-07-17 02:07:35

+0

這些全局變量並不是一個好主意。 – chris 2012-07-17 02:10:02

+1

在「if(ans ==」減去「/」乘「/」除「)」 你忘了「;」 「Operate Class opOper」 – 2012-07-17 02:10:47

回答

4

該代碼無效。您開始使用class OperateClass進行班級定義,但從未結束並直接進入main。 A(簡體)類定義的格式如下:

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! 

現在看來,這些可能只是複製/粘貼錯誤。如果是這種情況,那麼你可能只是在類定義的末尾忘記了分號。

+0

有一個隱藏的'if'。不過,它在那裏。 – chris 2012-07-17 02:03:48

+0

@chris:是的,這就是爲什麼我添加了最後一點。我認爲一個缺失的分號可能是問題,這只是一個錯誤的複製/粘貼作業。 – 2012-07-17 02:06:32

+0

好的。我要編輯原始代碼並添加分號。 – Bucky763 2012-07-17 02:07:58

1

考慮下面的程序:

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(){} 
+0

現在我得到_error:集合'OperateClass opOper'具有不完整的類型,無法定義_ – Bucky763 2012-07-17 02:13:08

+0

只要殺死類OperateClass;然後將整個班級移到那裏。 – chris 2012-07-17 02:22:53

+0

在main獲得程序運行之前移動類def! – Bucky763 2012-07-17 02:24:15

1

讓使你的代碼稍加改進,使您的語法是正確的。

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」也重複在每一種情況下,你也可以只寫你的發言如果其他條件之外,以避免重複,因爲無論任何情況下這種說法是正確的。

+0

謝謝,更改了代碼。 – Bucky763 2012-07-17 02:32:03