Thread: Linked List Problem

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    Linked List Problem

    Hello there! I am reacquainting myself with C/C++ after a few years of not practicing it. I am practicing Linked Lists but I cannot succeed in compiling a code. Take a look:

    Base Class Declaration
    Code:
    class Base
    {
        char *pstrName;              //STORE NAME
        char *pstrId;                //STORE ID
        static int iCount;           //KEEP TRACK OF THE CLASSES CREATED
        
        public:
            //CONSTRUCTOR(S) AND DESTRUCTORS
            Base();
            Base(char *Name, char *Id);
            virtual ~Base();
            
            //ACCESSOR METHODS
            char *ReturnName(); 
            char *ReturnId();
            int	  ReturnCount();
    
    		//LINKED LIST
    		class Base *next;
        };
    This is the code I am trying to create a linked list:

    Code:
    Base *LinkedBase1 = new Base();	
    
    Base *pHead = (Base*)0;
    
    pHead->next = LinkedBase1;
    Although when I attempt to compile the code it gives me an error....I can't figure it out...
    Code:
    ------ Build started: Project: Classes, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>c:\documents and settings\obliviouse\my documents\visual studio 2005\projects\classes\classes\main.cpp(15) : error C2143: syntax error : missing ';' before '->'
    1>c:\documents and settings\obliviouse\my documents\visual studio 2005\projects\classes\classes\main.cpp(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\obliviouse\my documents\visual studio 2005\projects\classes\classes\main.cpp(15) : error C2040: 'pHead' : 'int' differs in levels of indirection from 'Base *'
    Can someone help me? Thanks in Advance...
    Be easy on me...only 14

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The reason for the compiler errors might be due to superfluous class keyword here:
    Code:
    		//LINKED LIST
    		class Base *next;
    As to the rest:
    Code:
    Base *pHead = (Base*)0;
    
    pHead->next = LinkedBase1;
    The cast should be unnecessary. Any type of pointer should be able to be a NULL pointer. And on the next line you seem to be dereferencing a NULL pointer to set it's next member. However, pHead doesn't point to anything, so there is no next member.

    As to the design with classes, normally you'd separate a Node class/struct and a LinkedList class:
    Code:
    struct Node
    {
         data
         Node* next;
    };
    
    class LinkedList
    {
         Node* head;
         unsigned Count; // if you want
      
         public methods
    };
    Not making this distinction leads to this in your code:
    Code:
    static int iCount;           //KEEP TRACK OF THE CLASSES CREATED
    If I understand the purpose of iCount (NodeCount?) then you'd only be able to use only one linked list in a program (because of static).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM