《C++ primer》第五版 练习7.32 报错:“error C2797: 'Window_mgr::screens': 未执行成员初始值列表内的列表初始化或非静态数据成员初始值”

来源:互联网 发布:word怎么合计数据 编辑:程序博客网 时间:2024/05/22 12:16
练习7.32代码
#include<iostream>#include<string>#include<vector>using namespace std;class Screen;class Window_mgr;class Screen{public:friend class Window_mgr;typedef string::size_type pos;//构造函数Screen() = default;Screen(pos h, pos w) :height(h), width(w), contents(5, ' '){}Screen(pos h, pos w, char c) :height(h), width(w), contents(h*w, c){}//成员函数Screen &move(pos r, pos c);char get() const { return contents[cursor]; }char get(pos ht, pos wd) const;Screen &set(char);Screen &set(pos, pos, char);Screen &display(ostream &os){do_display(os); return *this;}const Screen &display(ostream &os) const{do_display(os); return *this;}private:pos cursor = 0;pos height = 0, width = 0;string contents;void do_display(ostream &os) const { os << contents; }};Screen& Screen::move(pos r, pos c){pos row = r*width;cursor = row + c;return *this;}inline char Screen::get(pos r, pos c) const{pos row = r*width;return contents[row + c];}inline Screen& Screen::set(char c){contents[cursor] = c;return *this;}inline Screen& Screen::set(pos r, pos col, char ch){contents[r*width + col] = ch;return *this;}class Window_mgr{public:friend Screen::Screen();using ScreenIndex = vector<Screen>::size_type;void clear(ScreenIndex);private:vector<Screen> screens{ Screen(84, 20, ' ') };};void Window_mgr::clear(ScreenIndex i){Screen &s = screens[i];s.contents = string(s.height*s.width, ' ');}int main(){Screen myScreen(5, 5, 'X');myScreen.move(4, 0).set('#').display(cout);cout << "\n";myScreen.display(cout);cout << "\n";Screen myScreen2(5, 5, 'Y');return 0;}

在VS2013的环境下进行编译,报错:“ error C2797: 'Window_mgr::screens': 未执行成员初始值列表内的列表初始化或非静态数据成员初始值”

在网上进行查找后,问题出在编译器上,VS2013不支持部分C++11特性。在类内部以列表方式初始化vector对象会出现此类错误。

关于类内初始化的内容,可以参考这篇博客:《C++11 FAQ中文版:类成员的内部初始化》http://blog.csdn.net/fjb2080/article/details/7527468


参考这篇帖子的说法:http://tieba.baidu.com/p/4574841989

“语法就是这样的 不是bug 只有静态类型才能在类里初始化 非静态类型只有创建实例的时候从构造函数里初始化”
类内初始化列表是0x新特性,之前的 类中初始化只能是 静态 常量类型的

对代码进行修改,使用构造函数在创建对象时对screens进行赋值。(类内初始化的时候只能为空)
#include<iostream>#include<string>#include<vector>using namespace std;class Window_mgr;class Screen{public:    friend class Window_mgr;    typedef string::size_type pos;    //构造函数    Screen() = default;    Screen(pos h, pos w) :height(h), width(w), contents(5, ' '){}    Screen(pos h, pos w, char c) :height(h), width(w), contents(h*w, c){}    //成员函数    Screen &move(pos r, pos c);    char get() const { return contents[cursor]; }    char get(pos ht, pos wd) const;    Screen &set(char);    Screen &set(pos, pos, char);    Screen &display(ostream &os)    {        do_display(os); return *this;    }    const Screen &display(ostream &os) const    {        do_display(os); return *this;    }private:    pos cursor = 0;    pos height = 0, width = 0;    string contents;    void do_display(ostream &os) const { os << contents; }};Screen& Screen::move(pos r, pos c){    pos row = r*width;    cursor = row + c;    return *this;}inline char Screen::get(pos r, pos c) const{    pos row = r*width;    return contents[row + c];}inline Screen& Screen::set(char c){    contents[cursor] = c;    return *this;}inline Screen& Screen::set(pos r, pos col, char ch){    contents[r*width + col] = ch;    return *this;}class Window_mgr{public:    friend Screen::Screen();    using ScreenIndex = vector<Screen>::size_type;    void clear(ScreenIndex);    Window_mgr() = default;    Window_mgr(vector<Screen> a){ screens = a; }//为向screens赋值,添加构造函数private:    //vector<Screen> screens{Screen(84,20,' ')};//VS2013不支持此类初始化方法    vector<Screen> screens{};};void Window_mgr::clear(ScreenIndex i){    Screen &s = screens[i];    s.contents = string(s.height*s.width, ' ');}int main(){       Screen myScreen(5, 5, 'X');       myScreen.move(4, 0).set('#').display(cout);       cout << "\n";       myScreen.display(cout);       cout << "\n";       Screen myScreen2(5, 5, 'Y');       vector<Screen> win{ myScreen, myScreen2 };       win[0].display(cout);       cout << "\n";       win[1].display(cout);       cout << "\n";       Window_mgr Win(win);       Win.clear(0);       return 0;}





阅读全文
0 0