Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用循环输出框_C++_Loops_Visual C++ - Fatal编程技术网

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&operator
box(inti,intj){i=x;j=y;}
您将其反转。如果正确使用初始化列表,则不会发生这种情况。