Thread: Is there a way to use class wtihout header file?

  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
    28,413
    What is wrong with including the header file? I hope your actual header file has inclusion guards.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM