Thread: Link List error

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    Link List error

    I can not figure out what is wrong with this header file.

    #ifndef USER_H
    #define USER_H

    struct Node
    {
    User user;
    Node * ptrNext;
    };

    class User
    {
    public:
    User();
    ~User();
    bool checkName( string name );
    bool checkPass( string pass );
    private:
    string mName;
    string mPass;
    Node * ptrHead;
    };

    #endif

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    struct Node
    {
    User user;
    Node * ptrNext;
    };


    When the compiler encounters the type User, it should give you an error because it has never seen that name before. It has no idea what you are referring to at that point. If you reverse the order of the struct and class declarations, you have the same problem because the name Node is in the class User. What to do? Think back to functions and function declarations, which are used for a similar situation.

    In the case of functions that call each other(or call each other indirectly through a third function), you need to put a declaration of each function at the top of the file, and then the compiler won't complain because it has seen all the possible names before it encounters them in the functions. If you make a habit of putting function declarations at the top of the file for each of your function definitions like they teach in beginning C++ texts, then you don't have to worry about the order you define your functions, and you don't even need to know the reason for doing it.

    The same type of solution applies to your header file problem. You need an incomplete User class declaration at the top of the file, so that the compiler has seen the name before it encounters it in the struct declaration:

    class User;

    struct Node
    {
    User user;
    Node * ptrNext;
    };

    class User
    {
    ...
    ...
    }

    That essentially tells the compiler that at some point you're going to declare a class called User and not to throw a hissy fit when it sees a variable of type User along the way. Unfortunately, the compiler is a very suspicious ogre, so if you don't include that incomplete class declaration at the top of the file, it automatically assumes you aren't ever going to declare the class, and in a regrettable act of paranoia it flags the use of a User variable as an error.
    Last edited by 7stud; 04-23-2003 at 03:27 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    4
    Actually I have tried to include the incomplete class declaration before the struct like you had explained but yet i get this error.

    'user' uses undefined class 'User'

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    It is case sensitive.
    Code:
    'user' uses undefined class 'User'
    It would have to be same the same
    Code:
    User  uses
    Or simply what 7stud posted.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You cannot declare objects of an incomplete class; you must use pointers or references.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    4
    Salem, thank you.

    It makes complete sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM