使用循环输出框

时间:2015-03-29 17:44:33

标签: 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, 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

编辑:我找到了错误并纠正了它,感谢所有人

1 个答案:

答案 0 :(得分:2)

我认为不是

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