我试图初始化一个指针 vector ,但是出现了一些错误,代码和错误都在下面
class tr
{
public:
int n = 26;
vector<tr* > a(n ,NULL);
};
我得到的错误是:
try_class.cpp:7:25: error: ‘n’ is not a type
vector<tr* > a(n ,NULL);
^
try_class.cpp:7:29: error: expected identifier before ‘__null’
vector<tr* > a(n ,NULL);
^~~~
try_class.cpp:7:29: error: expected ‘,’ or ‘...’ before ‘__null’
我不知道我的代码出了什么问题
最佳答案
不允许 parent 进行成员初始化(以避免大多数烦人的解析问题)。
您可以改为:
class tr
{
public:
int n = 26;
std::vector<tr*> a{std::size_t(n), nullptr};
};
要么
class tr
{
public:
int n = 26;
std::vector<tr*> a = std::vector<tr*>(n, nullptr);
};
关于c++ - 初始化用户定义类的指针 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422108/