Find the answer to your Linux question:
Results 1 to 7 of 7
Hi Ive programmed a LinkedList, but now i have to access the List within the elements. The problem is, that the List class has to have an include "elem.h" and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2003
    Location
    Emden, Germany
    Posts
    3

    LinkedList Elements accessing the List


    Hi

    Ive programmed a LinkedList, but now i have to access the List within the elements. The problem is, that the List class has to have an include "elem.h" and the elem class an include "List.h".
    Now i tried the following

    List.h:

    #ifndef _LIST_H
    # define _LIST_H
    # include "Elem.h"
    class List
    {
    Elem *first;
    .....
    }
    #endif

    Elem.h:

    #ifndef _ELEM_H
    # define _ELEM_H
    # include "List.h"
    class Elem
    {

    public:
    void dosomething(List *);
    }
    #endif

    But then i get the following compile error:

    Elem.hh:8: type specifier omitted for parameter `List'

    How could i solve the problem?
    I realy need accessing to the List.

    The Doc

  2. #2
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    1. I really wouldn't use underscores to begin any identifiers even if they are macros.
    2. You are missing the semi colon at the end curly brace for both classes
    3. How are you compiling this code? I'm assuming you have some cpp file that refers to these header files.
    The best things in life are free.

  3. #3
    Just Joined!
    Join Date
    Jun 2003
    Location
    Emden, Germany
    Posts
    3
    1. Im new to using preprozessor commands and this kind of define usage, so i just looked for a solution to get rid of the include loop and copy/pasted some code from a tarball . Why shouldnt i use an underscore at the beginning?
    2. I didnt copy/paste the code, so in the file its correct. just forgot it
    3. Im using GNU GCC 3.2 to make the .o files and then linking.
    Another class is instantiating a List Object an adding Elements, witch then go through a loop and calling "dosomething(List)". Each element should then check some things of the other Elements.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Why not bpark? I always name my header files macros like that, and the kernel source does the same.

    Anyway, try something like this (I'm not very good with C++, only with C, so I don't know if you can do this, but since C++ is based on C, you _should_ be able to do this):
    List.h:
    Code:
    #ifndef _LIST_H
    #define _LIST_H
    
    class List;
    
    #include "Elem.h"
    
    class List
    {
        Elem *first;
    };
    
    #endif
    Elem.h:
    Code:
    #ifndef _ELEM_H
    #define _ELEM_H
    
    class Elem;
    
    #include "List.h"
    
    class Elem
    {
        Elem *next;
    public:
        void dosomething(List *);
    };
    
    #endif

  6. #5
    Linux Engineer
    Join Date
    Nov 2002
    Location
    Queens, NY
    Posts
    1,319
    Huh, I always thought that we should stay away from identifiers beginning with underscores since some system variables are set that way.
    The best things in life are free.

  7. #6
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Yeah, but the general rule is that those that are of no real relevance to the program in general, or those symbols/macros that are more related to the linking process than to the program itself should have an underscore prefixed to them. It doesn't have to do with reserving them for the system.

  8. #7
    Just Joined!
    Join Date
    Jun 2003
    Location
    Emden, Germany
    Posts
    3
    It works.

    Thanks Dolda, bpark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •