C++ 创建类时,不能在返回类型中定义新类型

C++ 创建类时,不能在返回类型中定义新类型,c++,C++,我刚开始从学校的在线模块学习OOP和课程,模块中提供的对象出现了问题 这就是目标: //box.h class Box { public: //Properties char Color[]; int Length, Width, Height; //Methods Box(int length, int width, int height); int getVolume(); } Box::Box(int length, int wi

我刚开始从学校的在线模块学习OOP和课程,模块中提供的对象出现了问题

这就是目标:

//box.h
class Box
{
    public:
    //Properties
    char Color[];
    int Length, Width, Height;

    //Methods
    Box(int length, int width, int height);
    int getVolume();
}

Box::Box(int length, int width, int height)
{
    this.Width=width;
    this.Height=height;
}

Box::getVolume()
{
return this.Length * this.Width * this.Height;
}
这就是在main()中调用它的地方

#包括
#包括
#包括
#包括
#包括“box.h”
使用名称空间std;
int main()
{
盒子(2,3,4);

CUT< P> C++中的代码> < < /C> >是指向你所处的对象的指针,它不是一个引用,因此你需要做:

this->x
当访问属性
x
时,也就是说,您甚至不需要
this
部分。如果与局部变量或参数没有冲突,您只需
x

换句话说:

return Length * Width * Height;
请注意,在定义构造函数时,您应该朝着如下方向:

Box::Box(整数长度、整数宽度、整数高度):长度(长度)、宽度(宽度)、高度(高度)
{
}

很多错误,我已将您的代码清理为以下代码:

class Box
{
public:
    //Properties
    char Color[10]; //imcomplete type you have to specify the length i.e. char Color[10]
    int Length, Width, Height;

    //Methods
    Box(int length, int width, int height);
    int getVolume();
};

Box::Box(int length, int width, int height)
{
    this->Width = width;
    this->Height = height;
   this->Length = length; //you have to add this line otherwise the final result will be a garbage value
}

int Box::getVolume()
{
    return this->Length * this->Width * this->Height;
}
int main()
{
    Box box(2, 3, 4);
    std::cout << "The volume of our box is: ";
    std::cout << box.getVolume() << ".\n";
    system("pause");
    return EXIT_SUCCESS;
}
类框
{
公众:
//性质
字符颜色[10];//不完整类型您必须指定长度,即字符颜色[10]
整数长度、宽度、高度;
//方法
框(整数长度、整数宽度、整数高度);
int getVolume();
};
Box::Box(整数长度、整数宽度、整数高度)
{
这个->宽度=宽度;
这个->高度=高度;
this->Length=Length;//必须添加此行,否则最终结果将是一个垃圾值
}
int-Box::getVolume()
{
返回此->长度*此->宽度*此->高度;
}
int main()
{
盒子(2,3,4);

std::cout
this->
,而不是
this。
注意
this->
相同(*this)谢谢。我正准备在提供的链接列表中做更多的关于构造函数列表的阅读。我想知道为什么他们没有在构造函数中添加一个引用长度。谢谢你们解决这个问题。
class Box
{
public:
    //Properties
    char Color[10]; //imcomplete type you have to specify the length i.e. char Color[10]
    int Length, Width, Height;

    //Methods
    Box(int length, int width, int height);
    int getVolume();
};

Box::Box(int length, int width, int height)
{
    this->Width = width;
    this->Height = height;
   this->Length = length; //you have to add this line otherwise the final result will be a garbage value
}

int Box::getVolume()
{
    return this->Length * this->Width * this->Height;
}
int main()
{
    Box box(2, 3, 4);
    std::cout << "The volume of our box is: ";
    std::cout << box.getVolume() << ".\n";
    system("pause");
    return EXIT_SUCCESS;
}