BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #97083同步于 2018/1/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

【问题】看C++ Primer 第5版时遇到个问题

Dwohuaitwby
2018/1/11镜像同步5 回复
7.3.4节,讲友元的,令其他类的成员函数作为友元。 其中有两个类,Screen和Window_mgr,Window_mgr里包含一个vector<Screen>,同时有个函数clear对这个vector操作,书中说是先定义Window_mgr类,再定义Screen,最后定义clear函数,但是这样的话编译报错,因为vector使用了Screen,这个该咋解决啊? 这是相关的代码: class Screen; class Window_mgr; class Window_mgr { public: typedef string::size_type pos; using ScreenIndex = vector<Screen>::size_type; void Clear(ScreenIndex i); private: vector<Screen> Screens{ Screen(3,4,' ') }; }; class Screen { public: typedef string::size_type pos; Screen()=default; Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' ') {}; Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}; char get() const; inline char get(pos row, pos col) const; Screen &move(pos row, pos col); Screen &set(char c); Screen &set(pos row, pos col, char c); const Screen &display() const { do_display(); return *this; }; Screen &display() { do_display(); return *this; }; //friend class Window_mgr; friend void Window_mgr::Clear(ScreenIndex i); private: void do_display() const; pos cursor = 0; pos height = 0, width = 0; string contents; }; 错误出现在 vector<Screen> Screens{ Screen(3,4,' ') }; 这一行 error C2027: 使用了未定义类型“Screen”
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
z1973546机器人#1 · 2018/1/11
你在报错的那一行,还没有实现screen,所以不能初始化一个screen对象并用其初始化Screens,Screen(3,4,’’)只能写到Screen类的定义之后
Dwohuaitwby机器人#2 · 2018/1/12
哦,原来是后面那个screen的初始化不对。删掉就通过编译了。我之前以为是vector里面那个screen不对。。。谢谢指点! 【 在 z1973546 的大作中提到: 】 : 你在报错的那一行,还没有实现screen,所以不能初始化一个screen对象并用其初始化Screens,Screen(3,4,’’)只能写到Screen类的定义之后
mushroomboy机器人#3 · 2018/1/12
小哥哥你好棒 【 在 z1973546 的大作中提到: 】 : 你在报错的那一行,还没有实现screen,所以不能初始化一个screen对象并用其初始化Screens,Screen(3,4,’’)只能写到Screen类的定义之后
z1973546机器人#4 · 2018/1/12
sb 【 在 mushroomboy 的大作中提到: 】 : 小哥哥你好棒
zhongjiao机器人#5 · 2018/1/13
构造函数还没写呢,用不了