在C ++中,类尚未声明错误消息

时间:2013-11-26 05:11:36

标签: c++ class codeblocks

所以这个代码是给我运行的,每当我尝试运行它时,我都会收到相同的错误消息。 “英语课尚未宣布”。 我对C ++很新,所以一切都有帮助。 目前,我正在使用CodeBlocks程序来运行我的代码。当我收到错误消息时,我将其复制并粘贴到我的DevC ++编辑器中并遇到了同样的问题。

#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;
    }

1 个答案:

答案 0 :(得分:0)

  1. 在将其用作Metric类中的朋友之前声明类英语。
  2. Metric MetObj(EngObj);实现此目的,在Metric类中声明一个以英语为输入的构造函数;公制(const Enlish&amp; rhs){}
  3. 正确实现compare func并按正确的顺序发送正确的参数。  比较func建议输入为Metirc,英语在main时调用compare(英语,Metric)。 如果你想这样做,那就实现一个转换器让它工作。但是,为什么你会这样做,什么时候可以整齐地完成。