Trying to access a class member function


Why will it not let me do this:
int leastFrequency = myList[0].getFrequencyNumber();
or should it be int leastFrequency = myList[0]->getFrequencyNumber(); ?

The Intellisense does not even bring up the members of the Node class it brings up the members of a vector class when I do this: myList[0]. which is not what I want and I don't get why its doing that.

It is inside the void TreeFromListConstruction::formTree() function.

The error is:
: error C2039: 'getFrequencyNumber' : is not a member of 'std::vector<_Ty>'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


//Node.h
...
class Node
{
public:
	Node(){;}
	Node(Node child1, Node child2);
	int getFrequencyNumber(){return frequencyNumber;}

protected:
	int frequencyNumber;

private:
	vector <Node*> myChildren;

};

1
2
3
4
5
6
7
8
9
10
11
12
13
//TreeFromListConstruction.h
...
#include "LeafNode.h"

class TreeFromListConstruction
{
public:
	TreeFromListConstruction(vector<Node*> *aList);
private:
	vector<Node*> *myList;

};
...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//TreeFromListConstruction.cpp
#include "TreeFromListConstruction.h"

TreeFromListConstruction::TreeFromListConstruction(vector<Node*> *aList)
{
	myList = aList;
}

void TreeFromListConstruction::formTree()
{
	 // set least to first element
	int leastFrequency = myList[0].getFrequencyNumber();//ERROR!!!!!!!!!!!!!
	
}

1
2
3
4
5
6
7
8
9
10
//main.cpp
...
vector<Node*> LeafNodeList;
...
for(unsigned int i = 0; i < charByte.size(); i++)//putting the data into a LeafNode and the LeafNode into a list
	{
		LeafNodeList.push_back(new LeafNode(charByte[i], frequency[i]));
	}

	TreeFromListConstruction tree(&LeafNodeList);


Note LeafNode is a derived class of Node
myList is a pointer to vector of pointers to Node: vector<Node*> *myList
So to access Node members you should use something like this: (*myList)[n]->Node_Member
Last edited on
The Intellisense does not even bring up the members of the Node class it brings up the members of a vector class when I do this: myList[0]. which is not what I want and I don't get why its doing that.


That is because mylist is declared as follows:
vector<Node*> *myList;

Therefore myList[0] is a vector<Node*> - it is a vector (of Node pointers).

So for example - if you want the frequency of the Node at index 2 in the vector and remembering that a vector also has an operator[] overload - your code should be something like:

(mylist[0][2])->getFrequencyNumber();

Do you get what I'm saying?

Edit: Too slow - must learn to type faster.
Last edited on
Yes, it is odd to me that it is mylist[0][2] instead of mylist[2] but I definitely learned something today!
myList is a pointer to a vector, so you have to use some kind of dereference to get to the actual vector.

myList[0] works fine even though myList is itself not an array. Keeping with the definition of myList, however, it is more accurate to say

myList->operator[]( 2 )

to get to the 2nd element of the contained vector. However this syntax is clumsy and is one of the reasons why not to dynamically allocate STL containers .

Topic archived. No new replies allowed.