Results 1 to 4 of 4

Thread: Doesn't recognise my class

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Doesn't recognise my class

    This is a little hard to explain, but here goes.
    I have two classes which are defined in separate header files. For the sake of this message, I'm just call them 'Class1' and 'Class2'
    These two classes use each other, so I tried to do this:

    In Class1's header file
    Code:
    #include <Class2.h>
    In Class2's header file
    Code:
    #include <Class1.h>
    Both headers have '#pragma once' to avoid redefinition errors.

    The problem is then when the Class1 symbol is used in the Class2 header, it is not recognised (and vice verse).

    Maybe I've not explained this well enough, but I'll try to be more specific if you ask.
    Last edited by Barguast; May 30th, 2003 at 02:08 PM.
    Using Visual Studio .NET 2005

  2. #2
    Junior Member
    Join Date
    Sep 2002
    Location
    Kentucky
    Posts
    29
    Im a newbie to classes (programming in general, actually) so if this is off base please forgive me. Instead of using brackets <>, use quotation marks. Make sure the header files are in the same folder as the project, though.

    #include "myclass.h"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    sorry, I do use quotation marks,
    Code:
    #include "Class1.h"
    I'm still having bother with this, can anyone help?
    Using Visual Studio .NET 2005

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's quite tricky. One of the headers must contain a forward reference to the class from the other:
    Code:
    // header1
    
    #pragma once
    
    class class2;
    
    class class1
    {
    };
    
    // header2
    
    #pragma once
    
    #include "header1"
    
    class class2
    {
    };
    The question is: which should have the reference, and which should include the other?

    The answer is: if one of the two classes contains an object (not a pointer or reference) to the other, it needs the header included.
    If neither do, it doesn't matter.

    If each references members of the other in inline code, you must put it outside of the class like this:
    Code:
    // header1
    
    #pragma once
    
    class class2;
    
    class class1
    {
      void func1();
    };
    
    #include "header2"
    
    inline void class1::func1()
    {
    }
    
    // header2
    
    #pragma once
    
    #include "header1"
    
    class class2
    {
    };
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width