Compiling Order or Something??!

This is a discussion on Compiling Order or Something??! within the C++ Programming forums, part of the General Programming Boards category; I have two classes, class1 and class2. Both need to reference an object of the other class and it won't ...

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Compiling Order or Something??!

    I have two classes, class1 and class2. Both need to reference an object of the other class and it won't compile, i.e.

    Code:
    class Class1
    {
       public:
          void MyFunction(Class2 obj);
    };
    
    class Class2
    {
       Class1 obj;
    };
    I'm guessing the reason it doesn't compile is Class2 will not have been declared when the compiler reaches the "MyFunction(Class2 Obj)" line but I really need it to work!!

    Any help would be hugely appreciated.

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Try
    Code:
    class Class2;
    
    class Class1
    {
       public:
          void MyFunction(Class2 obj);
    };
    
    class Class2
    {
       Class1 obj;
    };
    If that doesn't work, then one of them will need to be a pointer to an object.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Or a reference (e.g. change the function declaration to: void MyFunction(const Class2& obj);)

  4. #4
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    The first one worked, thankyou v much.

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  2. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 05:26 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 10:46 AM

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