Thread: #include

  1. #1
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96

    #include

    Description of this problem is little longer, here it is:

    I have a console application under Visual C++ 6.0. It consists of main file "main.cpp" which uses a class defined in two files as usual: "someclass.h" and "someclass.cpp". Finally there is "library.h" file, which contains support functions (as an object too) for someclass (the class defined in someclass.x files).

    Files contain this code:
    library.cpp
    Code:
    //this is the library needed by someclass; implementation goes right after 
    //declaration here, all in one file; I use this (declaration & implementation
    //in one file) often and that's is not the problem...or it is?
    
    #pragma once  //doesn't help me
    
    #ifndef _AVOID_MULTIPLE_LINKING_  //even this
    #define _AVOID_MULTIPLE_LINKING_
    
    class AnyOtherClass {
      public: void method();
    };
    
    //linker reports: 
    //error LNK2005: "public: void __thiscall CSomeClass::method(void)" (?method@CSomeClass@@QAEXXZ) already defined in main.obj
    void AnyOtherClass::method() {
      {}
    }
    
    #endif
    someclass.h
    Code:
    //this is a header file for someclass
    
    #include "library.h"  //this library is needed by someclass
    
    //class is not really declared here
    //not even implemented in the following cpp file
    //that's just for simplicity
    //code for someclass - only declarations
    //...
    //...
    someclass.cpp
    Code:
    //this is a source file for someclass, so this line must be here:
    #include "someclass.h"
    
    //code for some class - implementation
    //...
    //...
    main.cpp
    Code:
    //finally I'd like to use someclass (which uses AnyOtherClass) in a console application like this
    #include "someclass.h"
    
    int main() {
      return 0;
    }
    
    //but I can't compile it...
    
    /*
    I found out this solution: Join someclass.h and someclass.cpp into one header file, keep this 
    file linked to main.cpp and keep the include directive ("library.h") on the top of it too - it works.
    
    But I'd like to know what is wrong and how to make it work with someclass.h and someclass.cpp files
    separately.
    */
    Please excuse my poor english...

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    I have found with MSVC++ any time you add a .cpp into your project it will try and compile it. I would get errors, undefined class, because the compiler did not compile the .h file yet, or i would get random linker errors
    When ever i use .h and .cpp files what i do is this

    Something.h
    #ifndef _something_H_
    #define _something_H_

    //blah blah

    #include "something.cpp"
    #endif


    Something.cpp
    #ifndef _something_CPP_
    #define _something_CPP_

    //blah blah

    #endif

    Then i include the .h file in my project, but leave the cpp out of the project. It turns up as an external dependancy in the project tab. Then in main.cpp i do

    main.cpp
    #include "something.h"
    //blah

    and it works.
    Try to not include any of the library.* files in your project and only include someclass.h in the project, and make sure to #include someclass.h in your main.

  3. #3
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    I use the precompiler directives as you can see from my code, but I thought compiler applies the directive to both .h a .cpp files it is in .h file. In system libraries, there are always ifndef, define, endif in header file. but I never saw implementations of them and maybe directives are there too. Interesting, I'll try it. Thanx.
    Please excuse my poor english...

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You either need to inline AnyOtherClass::method() or put the implementation in it's own cpp file.

    >#ifndef _AVOID_MULTIPLE_LINKING_ //even this
    #define _AVOID_MULTIPLE_LINKING_

    will prevent multiple inclusion in the same file. It wont prevent multiple linking if it's included in seperate cpp files.
    zen

  5. #5
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    That's it. My problem was that somebody told me to write simple classes in single header file (with implementation). Now I realized this is not good when putting a large number of such classes in one static library (as usually I do, because I haven't still learnt dynamic linking). Compiler needs to have everything with symbolic addresses in .cpp file, doesn't he? OK, now I always use .h and .cpp file even for small classes.
    But there is one problem. Let's say I need a stack which is independent on type. Some simple stack using linked lists but with templates. I've read that when you use templates in a static library, you must declare every template "version" you will use (at the end of implementation file of its class). Like:
    Code:
      class CMyStack<int>;
      class CMyStack<char>;
      // and so...
      // I think in MSDN Library they call it forward template declarations or something...
    But if I put in my static library and then somebody else will use this library, he can't declare an instance of CMyStack with let's say float type. Is there any way to do this?
    Please excuse my poor english...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:23 AM
  2. Socket programming
    By kahad in forum C Programming
    Replies: 3
    Last Post: 12-14-2006, 04:37 PM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  4. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM
  5. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 09:58 AM