39 52 D:\C语言资料包\Lab14_3.cpp [Error] in C++98 's1' must be initialized by constructor, not by '{...}'
时间: 2023-12-31 13:05:25 浏览: 20
这个错误是因为在旧版的 C++ 标准中,类中的成员变量不能使用花括号列表进行初始化,而必须使用构造函数进行初始化。你可以将类中的成员变量初始化放在构造函数中来解决这个问题。或者,你可以升级到较新的 C++ 标准(如 C++11、C++14、C++17 或 C++20),这些标准允许使用花括号列表进行成员变量初始化。
相关问题
leetcode27.cpp:29: error: in C++98 'nums' must be initialized by constructor, not by '{...}'
这个错误表示在 C++98 标准下,数组必须通过构造函数初始化,而不能使用 `{...}` 这种方式初始化。你可以尝试使用以下两种方法来解决这个错误:
1. 使用构造函数初始化数组:
```cpp
int nums[] = {1, 2, 3, 4, 5};
```
2. 将编译器设置为支持 C++11 标准或更高:
在 g++ 编译器中,可以使用 `-std=c++11` 或 `-std=c++14` 参数将编译器设置为支持 C++11 或 C++14 标准。例如:
```
g++ -std=c++11 your_program.cpp
```
9 79 D:\dev c++\cifa2.cpp [Error] in C++98 'grammar' must be initialized by constructor, not by '{...}'
这个错误提示是因为在 C++98 标准中,类的成员变量不能使用花括号来初始化,而必须使用构造函数。比如下面的代码就会导致这个错误:
```cpp
class Grammar {
public:
std::vector<std::string> nonterminals {"S", "A", "B", "C"};
std::vector<std::string> terminals {"a", "b", "c", "d", "e", "f"};
std::unordered_map<std::string, std::vector<std::vector<std::string>>> rules {
{"S", {{"A", "B", "C"}}},
{"A", {{"a", "A"}, {"b"}}},
{"B", {{"c", "B"}, {"d"}}},
{"C", {{"e", "C"}, {"f"}}}
};
};
```
解决这个问题的方法是,使用构造函数来初始化成员变量,例如:
```cpp
class Grammar {
public:
Grammar()
: nonterminals{"S", "A", "B", "C"},
terminals{"a", "b", "c", "d", "e", "f"},
rules{
{"S", {{"A", "B", "C"}}},
{"A", {{"a", "A"}, {"b"}}},
{"B", {{"c", "B"}, {"d"}}},
{"C", {{"e", "C"}, {"f"}}}
}
{}
std::vector<std::string> nonterminals;
std::vector<std::string> terminals;
std::unordered_map<std::string, std::vector<std::vector<std::string>>> rules;
};
```
这样就可以在 C++98 标准下正常编译了。当然,在 C++11 之后,使用花括号初始化成员变量是被允许的,所以如果你使用的是较新的编译器,也可以直接使用花括号初始化成员变量。