关于网友提出的“ 这个错在哪儿?”问题疑问,本网通过在网上对“ 这个错在哪儿?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 这个错在哪儿?
描述: 报错,c++primer上练习题12.13
class screen{
public:
typedef string::size_type index;
screen& move(index, index);
screen& set(char);
const screen& display(ostream&) const;
screen& display(ostream&);
private:
void do_display(ostream&) const;
string contents;
index cursor;
index width;
index height;
};
screen& screen::move(index a,index b){
index row = a * width;
cursor = row + b;
return *this;
}
screen& screen::set(char c){
contents[cursor]=c;
return *this;
}
void screen::do_display(ostream &os) const{
os<<>
}
const screen& screen::display(ostream &os) const{
do_display(os);
return *this;
}
screen& screen::display(ostream &os){
do_display(os);
return *this;
}
int main(){
screen myscreen;
myscreen.move(4,0).set('#').display(cout);
system("pause");
return 0;
}
解决方案1: 局部变量 screen myscreen; // 编译器不会负责将其初始化,使用的是脏数据
screen myscreen;
int main(){
myscreen.move(4,0).set('#').display(cout);
system("pause");
return 0;
}
这样修改也可以
解决方案2:#include
#include
using namespace std;
class screen{
public:
screen();
typedef string::size_type index;
screen& move(index, index);
screen& set(char);
const screen& display(ostream&) const;
screen& display(ostream&);
private:
void do_display(ostream&) const;
string contents;
index cursor;
index width;
index height;
};
screen::screen() : cursor(0), width(0), height(0)
{
}
screen& screen::move(index a,index b){
index row = a * width;
cursor = row + b;
return *this;
}
screen& screen::set(char c){
contents[cursor]=c;
return *this;
}
void screen::do_display(ostream &os) const{
os<<>
}
const screen& screen::display(ostream &os) const{
do_display(os);
return *this;
}
screen& screen::display(ostream &os){
do_display(os);
return *this;
}
int main(){
screen myscreen;
myscreen.move(4,0).set('#').display(cout);
system("pause");
return 0;
}
以上介绍了“ 这个错在哪儿?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3784937.html