Does declaring classes in the wrong order matter?

This is a discussion on Does declaring classes in the wrong order matter? within the C++ Programming forums, part of the General Programming Boards category; I have 2 classes, and within each there is a mention of the other. In the code, one of them ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    Does declaring classes in the wrong order matter?

    I have 2 classes, and within each there is a mention of the other. In the code, one of them will always have to appear above the other, and when that happens I get errors.

    Code:
    class controltype {
          public:
                 virtual void gettargetinfo(fighter *newtarget){};
    };
    
    class fighter: public spaceobject {
          public:
                 controltype *controller;
    };
    what can I do? Seems like a trivial problem to fix, but I can't get around it.

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    *edit* I am an idiot

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Enahs
    What errors do you get when your try to compile and link the following (I think this is what you are talking about).
    Not quite. In the op's example both classes refer to each other, so it doesn't matter which one you put first--the compiler will see an undeclared name in the first class.

    You need what's called a "forward declaration". Introduce the name of the second class above the first:

    class fighter;

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    No no no! I edited my mistake out before you posted! Now people will know what my stupid mistake was! Betrayal!

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    Thanks for the help guys, that worked a treat

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    No no no! I edited my mistake out before you posted!
    You're supposed to say: I NEVER POSTED THAT. IT'S A LIE! LOOK AT THE TIMESTAMP. YOU MADE THAT QUOTE UP! YOU CAN'T PROVE I POSTED THAT!

    Last edited by 7stud; 11-27-2005 at 12:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I calling this in the wrong order?
    By incognito in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 04:42 AM
  2. am I declaring my strings wrong?
    By sunburnbyRA in forum C++ Programming
    Replies: 7
    Last Post: 03-06-2003, 05:42 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. Wrong order when executing?
    By R@m0n in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2002, 06:04 AM
  5. Why did I get this question wrong?
    By DISGUISED in forum C++ Programming
    Replies: 6
    Last Post: 01-31-2002, 04:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21