Is there a way to use class wtihout header file?

This is a discussion on Is there a way to use class wtihout header file? within the C++ Programming forums, part of the General Programming Boards category; Hi For example i have .h and .cpp file that define my class like that: Code: //a.h class CExample { ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    Is there a way to use class wtihout header file?

    Hi
    For example i have .h and .cpp file that define my class like that:
    Code:
    //a.h
    class CExample {
      public:
        int a,b,c;
        CExample (int n, int m);
        void multiply()  ;
      };
    and
    Code:
    //a.cpp
    #include "a.h"
    CExample::CExample (int n, int m) { a=n; b=m; };
    void CExample::multiply () { c=a*b; };
    I want to use Cexample class object in another .cpp file without include a.h in the same project
    Like that:
    Code:
    //main.cpp
    #include "stdafx.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    	extern CExample c(5,4); //gives error.
    	return 0;
    }
    It gives error.
    Is there a way to use an object in another .cpp file (in the same project) without include its header file in c++?
    I tried extern CExample c(5,4);
    but doesnt work
    Thanks
    Last edited by sawer; 07-28-2007 at 12:35 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    What is wrong with including the header file? I hope your actual header file has inclusion guards.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you could write the class definition again in the source file, but then you'd have two things to change if you change your class.

    why don't you want to include the header file?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Thank you
    nothing wrong.
    I only want to learn if there is a way.
    I tried with extern keyword. But i think we cant use class with extern keyword.
    Am i wrong?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    extern is for instances, not types.

    I think you could do this if you are not going to access any of the members:
    Code:
    class MYclass;
    void func(Myclass *point)
    {
      point = point;
      return;
    }
    edit: yeah like laserlight said.
    Last edited by robwhit; 07-28-2007 at 12:02 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    You could use a forward declaration (e.g., class CExample; ) but not in this case since the class declaration is needed to instantiate an object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Quote Originally Posted by robwhit View Post
    extern is for instances, not types.
    Thank you but i dont understand what you mean.
    Could you explain this sentence.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by sawer View Post
    Thank you but i dont understand what you mean.
    Could you explain this sentence.
    an instance is when you make a variable. A type is what kind of variable it is.

    int a;
    a is an instance, and int is its type.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Yes
    so what is wrong with explicit CExample c(5,4)

    In MSDN says:
    "Objects and variables declared as extern declare an object that is defined in another translation unit or in an enclosing scope as having external linkage."

    CExample is a type c is a variable.
    What is wrong here?
    Thanks for all help

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the confusion here is about "what can be made extern and what cant".

    Types MUST be known by the compiler when their content is being used. Since a declaration of a class (or struct) in C++ involves calling the constructor for that class (and the constructor(s) of it's parent class(es)), the compiler must know what those constructors are and where these constructors are, and to know that, the compiler must know the entire content of the type, meaning that you have to include the header-file that contains that type (or some other way give the compiler the contents of the type - if you want to repeat the declaration of the class multiple times, that's fine too - but has the drawback of needing to be modified in several places if there's ever a need to modify the class).

    A variable (instance) can be declared extern, such as "extern CExample b;" - however, that tells the compiler that there is a declaration of this variable somewhere else. You can't initialize (give it values) here, that must be done at the "main declaration", which also where the constructor is being called.

    If you ever need to use the CONTENT of b as declared above, it will still need to be known to the compiler, so there's really no way around having it either included in the source code, or from a header-file.

    --
    Mats

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you tried to declare an instance (in main) simply as
    Code:
    CExample name(2, 3);
    ?

    explicit is a keyword to apply additional restrictions on constructors. extern is used when you have globals.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  2. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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