tags:

views:

86

answers:

2

I have an class and I would like to use the standard library list to store a list of them. I essentially want to push_front() the list. So my code is like this:

#include <list>
/* ... lots of stuff ...*/

complexNode myObject();

std::list<complexNode> complexList();

myList.push_front(myObject);

But the compiler throws this error:

error: request for member ‘push_front’ in ‘complexList’, which is of non-class type ‘std::list<complexNode, std::allocator<complexNode> > ()()’

The class complexNode has a copy contructor.

I really don't understand the problem and what that error actually means... please help!

+6  A: 

std::list complexList();

shouldn't this be :

std::list complexList; // without the ()

Max
Yes, with the empty () it's interpreted as a function declaration (!), even if it's in local scope.
bdonlan
Yes, if you use "std::list<complexNode> complexList();" the compiler thinks you are declaring a function complexList() returning a std::list<complexNode>.
lothar
Thanks that did the trick! :)
Juan Besa
@lothar: "the compiler thinks you are declaring a function". The compiler is correct ;-p
Steve Jessop
@onebyone: Yes, the user "thinks" he is declaring a variable and the compiler knows better ;-) This is a very common pitfall, though and IMHO the compilers should issue a warning if the user tries to "forward declare" a function inside a codeblock, as usually the user made a mistake and meant to declare a variable.
lothar
+4  A: 

This:

std::list<complexNode> complexList();

Has the common name "C++'s most vexing parse". In short, you have made complexList be the declaration of a function that returns a list, instead of a local variable. Remove the (), then it cannot be parsed as a function.

1800 INFORMATION