Thread: very basic linked list question (i think)

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    very basic linked list question (i think)

    just started learning linked lists, and i'm having some problems. was wondering if anyone could help me out...

    Code:
    //node.h file//////////////////////////
    #ifndef NODE_H
    #define NODE_H
    
    class NODE {
    	private:
       	int i;
          NODE *next;
    };
    
    #endif
    Code:
    //linkedlist.cpp (main .cpp) file/////////////////
    #include <iostream.h>
    #include <conio.h>
    #include "node.h"
    
    int main() {
    	NODE *first;
       NODE *temp=new NODE;
       int a=0;
    
       temp->i=100;
       temp->next=NULL;
       first=temp;
    
       a=first;
    
       cout << a;
    
       getch();
    
       return 0;
    }
    when i try to compile it, i get the following error messages:
    'NODE::i' is not accessible
    'NODE::next' is not accessible

    if anyone could help, i would really really appreciate it. thanks in advance. oh, and i'm using borland 5.02

    thanks again

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you made them private, therefore outside forces are not allowed to touch it. If you want to give outside forces the right to touch a member variable you have to make them public
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    but i'm accessing (or so i think i am) through an object of class NODE (*temp is declared as an object of NODE). or am i wrong?
    thanks

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    temp->i=100;

    this is being done in main() which is not a member of Node
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    got it, thanks a lot

  6. #6
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    A better way to write your list would be to hide how it really works behind an interface, that way you can give the user of your class an easy to use interface and you can change how the list works without crashing programs that already use it. You can even not use a linked list and it'll still work the same way.
    Code:
    #include <iostream>
    using namespace std;
    
    class NODE
    {
            int list_item;
            NODE *next;
    public:
            NODE( int new_item );
            void append( int new_item );
            NODE *go_next();
            void display_item();
    };
    
    // Make a node that links to nothing
    NODE :: NODE( int new_item )
    { list_item = new_item; next = 0; }
    
    // Add a node to the current node
    void NODE :: append( int new_item )
    { next = new NODE( new_item ); }
    
    // Go to the next node
    NODE *NODE :: go_next()
    { return next; }
    
    // Print the contents of the node
    void NODE :: display_item()
    { cout<< "Item: " << list_item <<endl; }
    
    int main()
    {
            NODE *head = new NODE( -1 ); // Dummy node
            NODE *list = head;
            
            // Append 20 numbers
            for( int add_item = 0; add_item < 20; add_item++ ) {
                    list->append( add_item );
                    list = list->go_next();
            }
    
            list = head->go_next(); // Skip the dummy node
            while( list != 0 ) {
                    list->display_item();
                    list = list->go_next();
            }
    
            // Destroy the list
            list = head->go_next(); // Skip the dummy node
            for( NODE *save = list; list != NULL; save = list ) {
                    list = save->go_next();
                    delete save;
            }
    
            return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  4. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  5. Simple linked list question
    By netboy in forum C Programming
    Replies: 3
    Last Post: 07-26-2002, 09:08 PM