24 65 C:\Users\B304\Documents\未命名1.cpp [Error] in C++98 'grid' must be initialized by constructor, not by '{...}'
时间: 2023-12-12 13:54:26 浏览: 162
这个错误是因为你在 C++98 标准下使用了类的成员初始化列表 `{...}`,但是这种写法只在 C++11 标准及以上版本中被支持。
要解决这个错误,你可以按照以下两种方式之一进行修改:
1. 将类的成员初始化放到构造函数中进行:
```cpp
class MyClass {
public:
MyClass(int x, int y, int z) {
grid = {x, y, z};
// other initialization code
}
private:
std::vector<int> grid;
};
```
2. 将编译器的标准切换到 C++11 或更高版本。例如,在 g++ 编译器中,可以使用 `-std=c++11` 或 `-std=c++14` 参数来开启 C++11 或 C++14 标准的支持。
相关问题
1 0 C:\Users\19746\Desktop\拉链1.cpp In file included from C:\Users\19746\Desktop\拉链1.cpp C:\Users\19746\Desktop\拉链1.cpp In function 'int main()': 10 16 C:\Users\19746\Desktop\拉链1.cpp [Error] array must be initialized with a brace-enclosed initializer
这个错误提示是因为你定义了一个数组,但是没有使用花括号将其初始化,而是直接使用了等号赋值的方式。在 C++ 中,使用等号进行的赋值只适用于一维数组且必须在定义时进行,而多维数组必须使用花括号进行初始化。
你可以将数组的定义和初始化改为类似下面的形式:
```c++
int a[5][5] = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}};
```
这样就能够正确地定义和初始化一个二维数组了。另外,你也需要检查代码中是否有其他的语法错误或者逻辑错误。
33 36 C:\Users\Administrator\Desktop\未命名1.cpp [Error] in C++98 'num' must be initialized by constructor, not by '{...}'
这条错误信息来自于在使用C++98标准编译时,编译器发现了一个构造函数初始化列表的错误。在C++98标准中,构造函数初始化列表用于初始化类的成员变量,其语法与C++11之后的标准有所不同。错误信息提示`num`成员变量没有被构造函数通过初始化列表正确初始化。
具体来说,C++98标准不支持直接使用花括号`{}`来初始化成员变量,而应该使用等号`=`或者在构造函数体内进行赋值操作。而在C++11及以后的标准中,支持了直接使用花括号的列表初始化方式。
例如,如果有一个类`Example`,其成员变量为`int num;`,在C++98中正确的构造函数初始化列表应该是这样的:
```cpp
class Example {
public:
int num;
Example() : num(0) { } // 使用等号进行初始化
// 或者
Example() { num = 0; } // 在构造函数体内赋值
};
```
而在C++11及之后的版本中,可以使用花括号直接初始化:
```cpp
class Example {
public:
int num;
Example() : num{} { } // 使用花括号列表初始化
};
```
如果你在C++98标准下编译,并且遇到了这个错误,你需要检查你的构造函数初始化列表,确保使用的是C++98支持的初始化方式。
阅读全文
相关推荐

















