2013-11-26 54 views
0

因此,此代碼已交給我運行,並且每當我嘗試運行它時,都會得到相同的錯誤消息。 「英語課沒有被宣佈」。 我對C++相當陌生,所以什麼都有幫助。 目前,我正在使用程序CodeBlocks來運行我的代碼。當我收到錯誤信息時,我將其複製並粘貼到我的DevC++編輯器中,並得到同樣的問題。類尚未在C++中聲明錯誤消息

#include <cstdlib> 
#include <iostream> 

using namespace std; 

class Metric { 
int meters; 
int centis; 
public: 
     Metric(int m, int cm) 
     { 
      meters = m; centis = cm; 
     } 
     Metric(void) { meters = centis = 0; } 

     void ShowMetricData(void) 
     { 
      cout << "Metric object: meters = " << meters; 
      cout << " centimeters = " << centis << "\n\n"; 
      } 


friend class English; 
friend bool compare(Metric, English); 


}; // code for class English follows this code 



class English { 
      int inches, feet; 
    public: 

// ‘Default Constructor 
     English(void) { feet = 0; inches = 0; } 

    // ‘Initializing Constructor 
     English(int ft, int in) { feet = ft; inches = in; } 

    // Prototype for a Constructor that converts a 
    // Metric instance into an English instance 
     English(Metric); 

    // Prototype for an operator function that converts 
    // an English instance into a Metric instance 
     operator Metric(void); 
     friend bool compare(Metric, English); 
     }; 


    /English::operator Metric() 
    // { 
    // Metric MObj; 
    // float MLgth, ELgth; 
    // ELgth = feet * 12 + inches; 
    // MLgth = 2.54 * ELgth; 
    // MObj.meters = (int) (MLgth/100.0); 
    // MObj.centis = (int) (MLgth - MObj.meters*100); 
    // cout << "In operator function Metric that converts English ==> Metric: \n\n"; 
    // cout << " English object: feet = " << feet 
    //    << " and inches = " << inches << "\n\n"; 
    // cout << " Metric object: meters = " << MObj.meters 
    //     << " cms = " << MObj.centis << "\n\n"; 
    // return MObj; 
    // } 
    // 
    // 
    // English::English(Metric Met_Obj) 
    //{ 
    // float MetL, EngL; 
    // MetL = 100.0 * Met_Obj.meters + Met_Obj.centis; 
    // EngL = MetL/2.54; 
    // 
    // feet = (int) (EngL/12.0) ; 
    // inches = (int) (EngL - 12*feet); 
    // 
    // cout << "In constructor English(Metric) that converts Metric=>English: \n\n"; 
    // cout << " Metric object: meters = " << Met_Obj.meters << " and cms = "<<  Met_Obj.centis << "\n\n"; 
    // cout << " feet = " << feet << " inches = " << inches << "\n\n"; 
    //} 
    // 
    //bool compare (Metric mObj, English eObj) 
    // { 
    //  float EobjLength; 
    //  float MobjLength; 
    // 
    //  EobjLength = eObj.feet * 12 + eObj.inches; 
    // 
    //  MobjLength = mObj.meters * 100 + mObj.centis; 
    // 
    //  if(MobjLength > EobjLength * 2.54) 
    //   return true; 
    //  else 
    //   return false; 
    //} 

/


    int main(int argc, char *argv[]) 
    { 
     English EngObj(8, 11); 
     Metric MetObj(EngObj); 

    //<<<<<<<<<< CODE BLOCK A >>>>>>>>>>> 
     if(compare(EngObj,MetObj)) 
      { 
      cout << "\n\n Metric object is larger\n\n"; 
      } 
      else 
      cout << "English object is larger \n\n"; 
     // <<<<<<<<< END OF CODE BLOCK A >>>>>>>>>>>>>>> 
     system("PAUSE"); 
     return EXIT_SUCCESS; 
    } 
+0

/English :: operator Metric(),在此處添加雙斜槓並重試 –

+0

取消註釋您的評論部分並在類Metric之前添加'class English;'。 –

回答

0
  1. 聲明類英語之前,使用它作爲度量類的朋友。
  2. Metric MetObj(EngObj);爲了實現這一點,在以英語作爲輸入的Metric類中聲明一個構造函數;公制(const Enlish & rhs){}
  3. 實現比較func並按正確的順序發送正確的參數。 由於比較func建議輸入爲Metirc,英文在主要調用比較(英文,公制)。 如果你想這樣做,然後實現一個轉換器來使這個工作。但是爲什麼你要這樣做,當事情可以做得乾淨利落。