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;
}
/English :: operator Metric(),在此處添加雙斜槓並重試 –
取消註釋您的評論部分並在類Metric之前添加'class English;'。 –