Reputation: 31
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;
}
Upvotes: 0
Views: 201
Reputation: 3795
Another option:
You can put the struct definitions outside the class and define class data members of their types (Item, Node) and initialize them in your class constructor. For example:
struct Item{
string key;
string value;
};
struct Node{
int count;
int maxSize;
vector<Item> items;
Node **branch;
...
};
class bTree
{
private:
int maxSize = 3;
Item item;
Node node;
public:
bTree(int size)
{
maxSize = size;
node.maxSize = size; // OR you may call a member function or constructor of Node struct.
...
}
~bTree(){}
// ...other methods...
};
Upvotes: 0
Reputation: 2373
The problem is that unlike java inner classes struct Node
doesn't have a pointer to outer class bTree
. That means that when the constructor of Node is called there is no visible variable bTree::maxSize to use. You have to explicitly pass maxSize as an argument to constructor. Another option is to make this variable static (but it looks for me that it is not suitable in your case because you want different maxSize for different instances of bTree).
EDIT: In case if you are interested how to use static field here:
class bTree
{
public:
static void setMaxSize(int maxSize)
{
bTree::maxSize = maxSize;
}
bTree()
{}
private:
static int maxSize = 3;
/* ... */
struct Node
{
/* ... */
Node()
: items(maxSize + 1)
, branch(new Node*[maxSize + 1])
{}
}
}
int main()
{
// bTree::setMaxSize(5);
bTree bTree;
return 0;
}
Upvotes: 0
Reputation: 35440
You are trying to access a member that is not in the scope of the Node
class. One fix is to so something similar to this:
struct Node{
//...
std::vector<Items> items;
Node(int m) : items(m + 1), branch(new Node*[m]()) {}
};
Another thing is that you should use std::vector
when possible. Your Node
class does not need to use dynamically allocated memory:
struct Node{
//...
std::vector<Item> items;
std::vector<Node*> branch;
Node(int m) : items(m + 1), branch(m) {}
};
Upvotes: 1