使用循环输出框

Using loops to output boxes

我的输出框代码,只输出空格。

#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;
}

这应该输出一些由 * 制成的盒子 但它唯一做的就是创建一些换行符和空格。 它甚至不打印 box bbox c

编辑:我发现了错误并更正了,感谢大家

我认为

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);