在这里,我试图用 key[2*d]
和 ptr[2*d+1]
创建一个结构,但是编译器给出了这些错误代码行只说:
"error:data members may not have variably modified type."
我只想在我的代码中使用“d”。现在如何修复它。
#include<iostream>
using namespace std;
static int d=1;
struct Btree{
public:
int key[2*d];
int count;
Btree *ptr[2*d+1];
Btree *pptr;
};
最佳答案
尝试使 d const,如果你让 C++11 使用 constexpr,编译器会感到不安,因为没有理由 d 不能随时更改。它将 T[N] 视为您看到的类型(这对优化很有用),因为您的 Btree 可能不完全相同而感到沮丧。
顺便说一下,您可以向 Btree 添加一个 int 模板参数,然后它会很高兴(假设整数是一个 constexpr),因为该模板中的所有内容都是相同的,但您不能混合使用它们(Btree<1> 和 Btree<2> 将无法交互,除非通过明确(通过模板或其他方式)同时使用它们的函数)
如果 d 可以改变,你真的希望它在堆上。
关于c++ - 使用全局变量给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19017296/