I'm porting over C code to C++ and I'm having an issue with dynamic array initialization. The code below is a simplified version of the problem. Why is there an error if maxSize is declared within the class yet it's fine if declared outside of it?
EDIT: Why isn't there a solution similar to the simplicity of adding static int maxSize; outside of the class? That's probably bad practice for reasons rarely mentioned so what would the next best solution be that requires the least amount of modification to the rest of the methods in the bTree class?
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
//int maxSize = 3;
class bTree
{
private:
int maxSize = 3; // this variable is fine outside of the class but not within it..
struct Item{
string key;
string value;
};
struct Node{
int count;
vector<Item> items;
Node **branch;
Node() : items(maxSize + 1) // error C2327: 'bTree::maxSize' : is not a type name, static, or enumerator
{
branch = new Node*[maxSize + 1]; // error C2327: 'bTree::maxSize' : is not a type name, static, or enumerator
}
};
// .... other variables....
public:
bTree(int size)
{
maxSize = size;
}
~bTree(){}
// ...other methods...
};
int main(int argc, char *argv[])
{
bTree *bt = new bTree(5);
return 0;
}