Reputation: 77
I have a class
class clsNode
{
private:
vector<clsNode*>m_Daughters;
However, this vector will always contain only 2 clsNode pointer. It should not be dynamic vector, but rather a fixed length array that can hold 2 pointers to 2 clsNodes.
I tried
vector<clsNode*>m_Daughters[2];
But that threw a lot of compiler errors.
Can somebody tell me how to do that correctly?
Upvotes: 2
Views: 3782
Reputation: 9063
vector<clsNode*>m_Daughters[2];
creates an array holding two elements of type vector<clsNode*>
. To correct this, you can:
declare the vector vector<clsNode*> m_Daughters;
and change the constructor of your class to clsNode() : m_Daughters(2, 0) {} // create a vector holding two objects of type clsNode*
no need to have a vector at all, just write clsNode* m_Daughters[2];
and change the constructor to clsNode() { m_Daughters[0] = m_Daughters[1] = 0; }
Upvotes: 2
Reputation: 56479
This definition has problem
vector<clsNode*> m_Daughters[2];
It makes m_Daughters
as an array of two vector<clsNode*>
which is far from your purpose.
To set the size, you can use its constructor
class clsNode
{
vector<clsNode*> m_Daughters;
public:
clsNode() : m_Daughters(2)
{}
};
If the size is constant, you can use std::array
:
class clsNode
{
array<clsNode*, 2> m_Daughters;
};
Upvotes: 3