Iteration inherited list

I have two classes inherited from the third class and they are stored in a list.

I am trying to iterate the list and call each The implementation function of a class, but the code cannot be compiled.

This is my code:

class A
{
public:

virtual void foo ()=0;
};

class B :public class A
{
public:< br />
void foo();
}

class C :public class A
{
public:

void foo();
}

std::list listOfClasses;

listOfClasses.push_back (new B());
listOfClasses.push_back (new C());

for(std::list::iterator listIter = listOfClasses.begin(); listIter != listOfClasses.end(); listIter++)
{
listIter->foo()
}

This code cannot be compiled, I receive the following error message (for the line listIter->foo()):< /p>

‘foo’: Not a member of’std::_List_iterator< _Mylist>‘

Any thoughts?

Your container contains pointers, so you need to dereference them:

(*listIter)->foo();

I have two classes inherited from the third class, they store In a list.

I am trying to iterate the list and call the implementation function of each class, but the code cannot be compiled.

This is my code:< /p>

class A
{
public:

virtual void foo ()=0;
};

class B :public class A
{
public:

void foo();
}

class C :public class A
{
public:

void foo();
}

std::list listOfClasses;

listOfClasses.push_back (new B());
listOfClasses.push_back (new C());

for(std::list::iterator listIter = listOfClasses.begin(); listIter != listOfClasses.end(); listIter++)
{
listIter->foo()
}

This code cannot be compiled, and I receive the following error message (for the line listIter->foo()):

‘foo’: not a member of’std::_List_iterator< _Mylist>‘

Any thoughts?

Your container contains pointers, so you need to dereference them:

(* listIter)->foo();

Leave a Comment

Your email address will not be published.