我有一個類Screen
其成員內容使用std::string(size_t, char)
構造函數初始化。不能使用字符串(size_t,char)的構造函數
#include <iostream>
#include <string>
struct Screen {
friend std::ostream& print(std::ostream& os ,const Screen& screen);
Screen() = default;
Screen(std::size_t w, std::size_t h) :
width(w), height(h), content(w * h , ' ') {}
Screen(std::size_t w, std::size_t h, char c) :
width(w), height(h), content(w * h, c) {}
private:
std::size_t width = 24;
std::size_t height = 80;
std::size_t cursor = 0;
std::string content(width * height, ' ');
};
我想宣佈在主裏面類似的方式爲字符串,但我得到了同樣的錯誤,我無法弄清楚我在做什麼錯在這裏。
structures.cpp:15:25: error: 'width' is not a type
std::string content(width * height , ' ');
^
structures.cpp:15:42: error: expected identifier before '\x20'
std::string content(width * height , ' ');
^
structures.cpp:15:42: error: expected ',' or '...' before '\x20'
爲什麼不使用初始化列表來構造'std :: string'?它試圖初始化你在哪裏聲明你的字符串的目的是什麼?'string content((width * height),'');'? – PaulMcKenzie