【发布时间】:2020-09-14 14:19:09
【问题描述】:
我想与操作员 '
我也不知道如何在函数GetBox()中返回多个值,我该如何解决?我想分别返回宽度、高度和深度。
下面是代码:
#include<iostream>
using namespace std;
class Box{
private:
int width;
int height;
int depth;
public:
Box():width(0),height(0),depth(0){};
Box(int w, int h, int d):width(w),height(h),depth(d){};
void BoxVolume();
void SetBox(int w1,int h1,int d1);
int GetBox();
friend ostream& operator<<(ostream &exit,const Box &A);
Box operator<(Box&);
};
void Box::SetBox(int w1,int h1,int d1){
width=w1;
height=h1;
depth=d1;
}
int Box::GetBox(){
return width,height,depth;
}
void Box::BoxVolume(){
cout<<"Volume: "<<width*height*depth<<endl;
}
ostream& operator<<(ostream &exit, const Box &B){
Box temp2;
exit<<B.width<<" "<<B.height<<" "<<B.depth<<" "<<endl;
return exit;
}
Box Box::operator<(Box &K){
}
int main(){
Box Box1;
cout<<"Details about first box:"<<endl;
Box1.SetBox(1,3,5);
Box1.GetBox();
cout<<Box1;
Box1.BoxVolume();
cout<<endl;
Box Box2;
cout<<"Details about second box:"<<endl;
Box2.SetBox(2,4,6);
Box2.GetBox();
cout<<Box2;
Box2.BoxVolume();
}
【问题讨论】:
-
从
operator <返回Box而不是true或false没有意义。 -
你想从哪个函数返回多个值?解决这个问题的方法可能会有所不同。
-
请解释你想要
operator<到意思对于Box。 -
@Yksisarvinen 看看他的 GetBox 方法。
-
哦,对了。
std::array<int, 3>作为返回类型将是一个选项,但此时你为什么不直接将width、height和depth公开?这些成员已经暴露了。
标签: c++ operator-keyword