Hello, I'm making a linked list and when i try to make a member function to return a new object
i keep getting an error reading: "‘node’ does not name a type". I think im writing the function
wrong, any help would great.

#include <iostream>

class list
{
   private:
      class node
      {
         public:
            int data;
            node *next;
      }*head;

   public:
      list() : head(0) {}
      ~list();
      bool isempty() const;
      void print() const;
      void push_back(int);
      node *new_node(int); 
};

//function to return a new node
node *list::new_node(int v) ///////////error here
{
   node *temp = new node;
   temp->data = v;
   temp->next = 0;

   return temp;
}

//destructor
list::~list()
{
   node *temp = head;
   node *tmp;

   while(temp != 0)
   {
      tmp = temp;
      temp = temp->next;
      delete tmp;
   }
}

//check if list is empty
bool list::isempty() const
{
   return head == 0;
}

//print list
void list::print() const
{
   node *temp = head;

   while(temp != 0)
   {
      std::cout << temp->data << std::endl;
      temp = temp->next;
   }

   std::cout << std::endl;
}

//push back into list
void list::push_back(int v)
{
   node *temp = new_node(v);

   if(isempty())
      head = temp;
   else
   {
      temp->next = head;
      head = temp;
   }
}

int main()
{
   using namespace std;

   list test;

   test.push_back(1);
   test.push_back(2);
   test.push_back(3); 


   test.print();


   return 0;
}

Recommended Answers

All 4 Replies

There is the error to here.

list::node *list::new_node(int v) ///////////error here
{
    node *temp = new node;
    temp->data = v;
    temp->next = 0;
    return temp;
}

If the return type was the list::node, the compiler would not known the node type.

Should your class name be called "list"...?

What kind of error are you receiving, run-time or compile time? May I know the IDE you are using and its version?

thank you acecode, i needed to add "list::node".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.