这是我的文件
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
主文件
#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
但事实是这样的列表初始化在我的两个类外的尝试中正常运行(参见 main.cpp)。我以为是因为private
修饰符,但public
没有多大帮助。所以你能建议我吗,真正的问题在哪里?谢谢!
UPD我使用 MS VS Expess 2013 和 CTP 2013 VC++ 编译器。