We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,464 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Error with a class function

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;
}
4
Contributors
4
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
6
Views
Question
Answered
fishsticks1907
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

acecode
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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

phorce
Master Poster
746 posts since Jul 2011
Reputation Points: 63
Solved Threads: 94
Skill Endorsements: 16

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

RonalBertogi
Junior Poster in Training
50 posts since Nov 2012
Reputation Points: 26
Solved Threads: 11
Skill Endorsements: 0

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

fishsticks1907
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by RonalBertogi, acecode and phorce

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0683 seconds using 2.69MB