Acessing objects functions and atributes on a vector thought an Iterator

Hello,

I am new to c++ but have some experience on c. I am trying to create instancies of a Class and store then on a vector. Then I would like to iterate throught the vector, to acess some of the objects functions and atributes.

Here is how I am doing it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

Node.h:

Class Node
{

protected:

   int id;

public:
  
   int getId();
}


1
2
3
4
5
6
7
8
Node.cpp:

//constructors..

int Node::getId()
{
   return id;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
main.cpp:

vector<Node *> nodes;

//in a loop, I fill the vector like this:

  nodes.push_back(new Node(a, b, c, d));

//Then I try to iterate tought the vector, and access the getId() method:

for (vector<Node *>::iterator it=nodes.begin(); it!=nodes.end(); it++)
{
    printf("%d", *it.getId());
}


But when I try to access the members of the array getId() function throught the Iterator, I got the following error:

 
error:'class__gnu_cxx::__normal_iterator<Node**, std::vector<Node*, std::allocator<Node*> > >' has no member named 'getId'


So I have two questions:

1- Am I creating the Node objects instances and inserting then on the vector correctly?

2- How can I access functions or atributes of members of a vector of a given class? (I would like to be able to access the getId() methods of each member of the vector, for example)

Thank you in advance, let me know if you need some more information.

Class needs to be not capitalized.

2-vector.at(position).getId();

-Albatross
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>

class Node
{
    int id;
public:
    Node(int value) : id(value)
    {
    }
    ~Node()
    {
    }
    int getId() const { return id; }
};

int main()
{
    std::vector<Node*> nodes;

    //in a loop, I fill the vector like this:
    nodes.push_back(new Node(5));

//Then I try to iterate tought the vector, and access the getId() method:

    for (std::vector<Node*>::iterator it = nodes.begin(), end = nodes.end(); it != end; it++)
    {
	//printf("%d", *it.getId());
	std::cout << "Node ID = " << (*it)->getId() << std::endl;
    }
    return 0;
}


The iterator is a pointer to an element of the vector which in turn points to a Node object. There might be another way to do it but if there is I am not sure how.
Thank you for the quick answers! Kempo sugestion worked for me!
Topic archived. No new replies allowed.