C++ crew: Question for you.

Discussion in 'OT Technology' started by Joe_Cool, Nov 10, 2004.

  1. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,206
    Likes Received:
    516
    In my programming class, we're manually implementing linked lists. Basically I need to make a series of objects of class Blah, and each object needs to contain a pointer to the Blah that comes before it and the one that comes after it.

    But when I compile, I get this error:
    c:\c++\prog5\Blah.h(5) : error C2460: 'prev' : uses 'Blah', which is being defined
    c:\c++\prog5\Blah.h(2) : see declaration of 'Blah'

    So am I doing something wrong here, or am I not allowed to have class Blah contain a pointer to Blah? I thought it should be no problem. Any ideas?
     
    Last edited: Nov 10, 2004
  2. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,206
    Likes Received:
    516
    Nevermind. It was a stupid error, as usual.

    In my class definition, I had declared the pointers like this:
    Blah *prev,next;

    instead of this:
    Blah *prev, *next;

    :uh:
     
  3. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    :hsugh: done that before
     
  4. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Yep, I think we all have.
     
  5. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,206
    Likes Received:
    516
    Yeah it pretty well sucks. The compiler caught me from an infinite, memory-eating loop, since my class had itself contained in itself, instead of a pointer to itself. :mamoru:
     
  6. SL1200MK4

    SL1200MK4 New Member

    Joined:
    Sep 27, 2003
    Messages:
    1,552
    Likes Received:
    0
    Also word of advice, use foward declearation whenever possible...
     
  7. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,206
    Likes Received:
    516
    What is "forward declaration"?
     
  8. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,206
    Likes Received:
    516
    Oh, I see. Like function prototyping, but for classes, right?
     

Share This Page