【发布时间】:2021-03-05 22:27:15
【问题描述】:
我有以下代码。
constexpr int w=50;
constexpr int h=50;
struct Canvas {
std::vector<std::vector<char>> net( w, std::vector<char>(h) );
void clear(const char clear_char = ' ') {
for (int i = 0; i < net.size(); ++i)
for (int j = 0; j < net[i].size(); ++j)
net[i][j] = clear_char;
}
void draw_circle(const Brush& brush, const int cx, const int cy, const int r){
static int c = 0;
for (double i = 0; i <= 6.28; i += 0.02) {
int y = round(0.54*r * sin(i))+cy;
int x = round(r * cos(i)) + cx;
if (c >= 0 && c <= 1)
net[y][x] = brush.outline;
else
net[y][x] = brush.fill;
}
if (c != 0 && r == 0) {
c = 0;
}
else if(brush.fill!='\0'){
++c;
draw_circle(brush, cx, cy, r - 1);
}
}
};
我想创建一个 2D 字符向量(wxh 维度),但我的编译器指出了以下错误:
E0757: variable w is not a type name.
如何解决?
【问题讨论】:
-
@KarenBaghdasaryan 这很好用。您能否提供使用该行的完整代码。
-
我提供了
-
(...)不允许对类范围内的成员进行初始化。您必须使用{...}或= ...,例如std::vector<std::vector<char>> net = std::vector<std::vector<char>>( w, std::vector(h) );. -
@HolyBlackCat 非常感谢。
-
顺便说一句,以浮点相等测试结束的循环不是一个好主意,因为可能存在微小的方向错误,使其过早停止一次迭代
标签: c++ vector initialization