C++ 使用循环输出框
我的代码用于输出框,只输出空格C++ 使用循环输出框,c++,loops,visual-c++,C++,Loops,Visual C++,我的代码用于输出框,只输出空格 #include <iostream> using namespace std; class box{ int x, y; public: box(int i, int j){ x = i; y = j; } friend ostream &operator<<(ostream &stream, box o); }; ostream &operator<<(ostream &stream,
#include <iostream>
using namespace std;
class box{
int x, y;
public:
box(int i, int j){ x = i; y = j; }
friend ostream &operator<<(ostream &stream, box o);
};
ostream &operator<<(ostream &stream, box o)
{
register int i, j;
for (i = 0; i < o.x; i++)
stream << "*";
stream << "\n";
for (j = 1; j < o.y-1; j++){
for (i = 0; i < o.x; i++)
if (i == 0 || i == o.x - 1) stream << "*";
else stream << " ";
stream << "\n";
}
for (i = 0; i < o.x; i++)
stream << "*";
stream << "\n";
return stream;
}
int main(){
box a(14, 6), b(30, 7), c(40, 5);
cout << a << b << c;
return 0;
}
#包括
使用名称空间std;
类框{
int x,y;
公众:
盒(inti,intj){x=i;y=j;}
friend ostream&operator我认为
box(int i, int j){ i = x; j = y; }
你是说
box( int i, int j ) : x( i ), y( j ) {}
最好像这样声明操作符
friend ostream &operator<<(ostream &stream, const box &o);
friend ostream&operatorbox(inti,intj){i=x;j=y;}
您将其反转。如果正确使用初始化列表,则不会发生这种情况。