c++;重载函数故障 我正在学习C++中的重载函数,书中的一个例子将无法工作。这是密码 #include <iostream> using namespace std; //rectangle class declaration class Rectangle { public: //constructors Rectangle(int width, int height); ~Rectangle(){} //overload class function drawShape void DrawShape() const; void DrawShape(int aWidth, int aHeight) const; private: int itsWidth; int itsHeight; }; //constructor implementation Rectangle::Rectangle(int width, int height) { itsWidth; itsHeight; } //overloaded DrawShape - takes no values //Draws based on current class member values void Rectangle::DrawShape() const { DrawShape(itsWidth, itsHeight); } //overloaded DrawShape - takes two values //Draws shape based on the parameters void Rectangle::DrawShape(int width, int height) const { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { cout << "*"; } cout << "\n"; } } //Driver program to demonstrate overloaded functions int main() { //Initialize a rectangle to 30, 5 Rectangle theRect(30, 5); cout << "DrawShape(): \n"; theRect.DrawShape(); cout << "\nDrawShape(40, 2): \n"; theRect.DrawShape(40, 2); system("pause"); return 0; } #包括 使用名称空间std; //矩形类声明 类矩形 { 公众: //建设者 矩形(整数宽度、整数高度); ~Rectangle(){} //重载类函数drawShape void DrawShape()常量; 空心拉伸形状(内宽、内高)常数; 私人: 宽度; 高度; }; //构造函数实现 矩形::矩形(整数宽度、整数高度) { 宽度; 它的高度; } //重载DrawShape-不接受任何值 //基于当前类成员值绘制 空心矩形::DrawShape()常量 { 图纸形状(其宽度、高度); } //重载DrawShape-接受两个值 //根据参数绘制形状 空心矩形::DrawShape(整数宽度、整数高度)常量 { 对于(int i=0;i

c++;重载函数故障 我正在学习C++中的重载函数,书中的一个例子将无法工作。这是密码 #include <iostream> using namespace std; //rectangle class declaration class Rectangle { public: //constructors Rectangle(int width, int height); ~Rectangle(){} //overload class function drawShape void DrawShape() const; void DrawShape(int aWidth, int aHeight) const; private: int itsWidth; int itsHeight; }; //constructor implementation Rectangle::Rectangle(int width, int height) { itsWidth; itsHeight; } //overloaded DrawShape - takes no values //Draws based on current class member values void Rectangle::DrawShape() const { DrawShape(itsWidth, itsHeight); } //overloaded DrawShape - takes two values //Draws shape based on the parameters void Rectangle::DrawShape(int width, int height) const { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { cout << "*"; } cout << "\n"; } } //Driver program to demonstrate overloaded functions int main() { //Initialize a rectangle to 30, 5 Rectangle theRect(30, 5); cout << "DrawShape(): \n"; theRect.DrawShape(); cout << "\nDrawShape(40, 2): \n"; theRect.DrawShape(40, 2); system("pause"); return 0; } #包括 使用名称空间std; //矩形类声明 类矩形 { 公众: //建设者 矩形(整数宽度、整数高度); ~Rectangle(){} //重载类函数drawShape void DrawShape()常量; 空心拉伸形状(内宽、内高)常数; 私人: 宽度; 高度; }; //构造函数实现 矩形::矩形(整数宽度、整数高度) { 宽度; 它的高度; } //重载DrawShape-不接受任何值 //基于当前类成员值绘制 空心矩形::DrawShape()常量 { 图纸形状(其宽度、高度); } //重载DrawShape-接受两个值 //根据参数绘制形状 空心矩形::DrawShape(整数宽度、整数高度)常量 { 对于(int i=0;i,c++,function,overloading,C++,Function,Overloading,你的构造函数没有做任何事情,它应该是 //constructor implementation Rectangle::Rectangle(int width, int height) { itsWidth = width; itsHeight = height; } 由于DrawShape是类Rectangle中的一个函数,它可能不应该接受任何参数,它应该使用对象的成员变量 void Rectangle::DrawShape() const { for (int i =

你的构造函数没有做任何事情,它应该是

//constructor implementation
Rectangle::Rectangle(int width, int height)
{
    itsWidth = width;
    itsHeight = height;
}
由于
DrawShape
是类
Rectangle
中的一个函数,它可能不应该接受任何参数,它应该使用对象的成员变量

void Rectangle::DrawShape() const
{
    for (int i = 0; i < itsHeight; i++)
    {
        for (int j = 0; j < itsWidth; j++)
        {
            cout << "*";
        }
        cout << "\n";
    }
}

您的构造函数没有初始化任何内容,应该是这样的:

//constructor implementation
Rectangle::Rectangle(int width, int height)
    : itsWidth(width)
    , itsHeight(height)
{
}
DrawShape(): 
******************************
******************************
******************************
******************************
******************************

DrawShape(40, 2): 
****************************************
****************************************
更改后,您的结果如下所示:

//constructor implementation
Rectangle::Rectangle(int width, int height)
    : itsWidth(width)
    , itsHeight(height)
{
}
DrawShape(): 
******************************
******************************
******************************
******************************
******************************

DrawShape(40, 2): 
****************************************
****************************************

最大限度地启用编译器警告,并注意那些说明“表达式结果未使用”性质的警告。我做了,没有其他警告。此类警告的示例:我更改了代码以反映您的代码,但仍然存在相同的问题,初始化为30,5时它没有绘制第一个矩形。谢谢,我更改了它,但仍然存在相同的问题。它对我来说工作正常,我在mac上使用clang600编译并运行它,您确定above是您正在使用的确切代码?是的,我使用的是visual studio 2013,该代码是使用您的代码和cyber的直接复制和粘贴