You are here:

C++/Linked Lists


Hi, I was working on a Linked lists program, and these errors come up that I cannot work out. I have a "grocery list" That I am supposed to add to, take away from, and print. But it comes up with these errors:

request for member `push_back' in `grocery', which is of non-class type `List ()()'
conversion from `List (*)()' to non-scalar type `List' requested

Here is the main code for it:
#include <iostream>
#include "List.h"
using namespace std;

void printList(List current){
  Node* printer = current.get_first();
  string g = "pickles";
  while (printer->get_next() != 0)
     cout << printer->get_quantity() << " " << printer->get_item() << endl;
     printer = printer->get_next();

int main()
Node* one = new Node("1 gallon of", "milk");
List grocery();
grocery.push_back(one); /* This is where I get the first error */
printList(grocery); /* Second error */

/* List code */
  first /* pointer tofirst node in the list */ = last /* pointer to last node in the list */ = 0;
  size /* size of list */ = 0;

void List::push_back(Node* pusher1) /* Adds a node to the bottom of the list */
  if (last = 0)
     first = last = pusher1;
     last->set_next(pusher1); /* sets pointer to next node in the list */
     last = pusher1;

I don't get any errors in my List.h or List.cpp, only in the driver.
Thanks for your help.

Ok this is the line where you made the (reasonable) mistake:

   List grocery();

You might think that this creates a List object called grocery initialised using the List class default constructor. Unfortunately this is not the case. One of the most irritating rules of C++ (possibly inherited from C) is that anything that can be parsed as a function declaration will be. That is if a statement can in anyway be considered to declare a function (i.e. look like a function prototype) then this is what the compiler will assume it is. And this is exactly what the compiler has done here. If you saw this statement elsewhere, in a header file for example:

       List grocery();

Then you would assume it to be a declaration of a function called grocery that takes no parameters and returns a List object.

Unfortunately a function can also be declared within a function definition and by the rule that if it looks like a function declaration the compiler will treat is as such means that that is what the compiler assumes your statements means: that it declares a function called grocery rather than default initialise an object called grocery.

Luckily the fix is easy - remove the parentheses:

   // correct way to default initialise List object grocery
   List grocery;  

You can see that the compiler was talking about a function from all those parentheses in the errors:

   List ()()

This is the type of the function it thought you were declaring without the name. With the name it would be:

   List (grocery)()


   List (*)()

Is a pointer to a function of the same type. The additional parentheses are there because of order of precedence issues. For example the pointer type modifier * is in () so as it binds to the function rather than the list - a pointer to function returning List rather than a function returning a pointer to list.

Hope this helps.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.