Thread: Multiple file handeling

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Multiple file handeling

    I have three files
    1. space1.hpp 2. space2.hpp and 3. main.cpp
    space1.hpp contains:
    Code:
    using namespace std;
    namespace space1        {
            class sp1{
    
                    public:
                    sp1(){
                    }
    
            };
    }
    space2.hpp contain
    Code:
    #include "space1.hpp"
    // want to use sp1 class here
    using namespace std;
    using namespace1;
    namespace space2        {
            class sp2{
    
                    public:
                    sp2(sp1 sp){
                    }
    
            };
    }
    & main.cpp contains:
    Code:
    #include "space1.hpp"
    #include "space2.hpp"
    // want to use both the class sp1 and sp2 here
    using namespace std;
    using namespace space1;
    using namespace space2;
    
    int main(){
            sp1 s1 =sp1();
            sp2 s2 =sp2(s1);
    }
    Error is coming:
    Code:
    In file included from space2.hpp:1,
                     from main.cpp:2:
    space1.hpp:4: error: redefinition of ‘class space1::sp1’
    space1.hpp:4: error: previous definition of ‘class space1::sp1’
    In file included from main.cpp:2:
    space2.hpp:5: error: expected nested-name-specifier before ‘namespace1’
    space2.hpp:5: error: ‘namespace1’ has not been declared
    space2.hpp:11: error: expected ‘)’ before ‘sp’
    main.cpp: In function ‘int main()’:
    main.cpp:10: error: no matching function for call to ‘space2::sp2::sp2(space1::sp1&)’
    space2.hpp:8: note: candidates are: space2::sp2::sp2()
    space2.hpp:8: note:                 space2::sp2::sp2(const space2::sp2&)
    kindly fix it!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    kindly fix it!
    Hmmm, there are better ways to ask for assistance rather than making demands.

    #1. Learn about header inclusion guards and make use of them in your header files. This will help avoid those pesky "redefinition" errors you are getting.

    #2. Avoid blanket "using namespace" declarations in your header files. They open up that entire namespace to any source file that just happens to include said headers. Within the headers, try to always explicitly qualify the namespace of any objects which require it. If you want to put in a using namespace declaration, then do it at the source level and not the header level.

    Doing the above, your two header files should look like the following:
    Code:
    #ifndef SPACE1_H
    #define SPACE1_H
    
    namespace space1 {
        class sp1 {
        public:
            sp1() {
            }
        };
    }
    
    #endif
    Code:
    #ifndef SPACE2_H
    #define SPACE2_H
    
    #ifndef SPACE1_H
    #include "space1.hpp"
    #endif
    
    namespace space2 {
        class sp2{
        public:
            sp2(space1::sp1 sp){
            }
        };
    }
    
    #endif
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM