C ++ 11列表初始化不起作用

时间:2014-04-04 17:02:33

标签: c++ c++11 vector visual-studio-2013 list-initialization

这是我的档案
screen.h

#ifndef SCREEN_H
#define SCREEN_H


class Screen {
public:
    using pos = std::string::size_type;
    Screen() = default;
    Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht*wd, c) {}
    char get() const { return contents[cursor]; }
    char get(pos r, pos c) const;
    Screen &move(pos r, pos c);

private:
    pos cursor = 0;
    pos height = 0, width = 0;
    std::string contents;
};

inline char Screen::get(pos r, pos c) const {
    pos row = r*width;
    return contents[row + c];
}

inline Screen &Screen::move(pos r, pos c) {
    pos row = r*width;
    cursor = row + c;
    return *this;
}


class Window_mgr {
private:
public:
    std::vector<Screen> screens = { Screen(24, 80, ' ') };

};

#endif

的main.cpp

#include <iostream>
#include <string>
#include <vector>

#include "screen.h"

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main() {
    Window_mgr w;
    w.screens.push_back(Screen(24, 80, ' '));

    std::vector<Screen> screens = { Screen(24, 80, ' ') };
}

问题出在Window_mgr class内的列表初始化行中。编译说:

"std::vector<Screen,std::allocator<_Ty>>::vector(std::initializer_list<Screen>,const std::allocator<_Ty> &)": 
невозможно преобразовать аргумент 1 из "Screen" в "const std::allocator<_Ty> &" 
d:\users\family\documents\visual studio 2013\projects\consoleapplication\screen\screen.h

但事实是这样的列表初始化在类外的两个tryes中正常运行(参见main.cpp)。我认为这是因为private修饰符,但public没有帮助。 所以,你能告诉我,真正的问题在哪里?谢谢!

UPD 我使用MS VS Expess 2013和CTP 2013 VC ++编译器。

0 个答案:

没有答案