1.11M Members

Error with a class function

 
0
 

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;
}
 
0
 

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.

 
0
 

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

 
0
 

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

 
0
 

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

Question Answered as of 1 Year Ago by phorce, RonalBertogi and acecode
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article