0

I have this code:

in a.h file:

#ifndef A_H_
#define A_H_

class Class_Hierarchy{
public:
    std::map<const Type*, std::set<const Type*>> map;
    Class_Hierarchy(){
            std::map<const Type*, std::set<const Type*>> map;
    }
 };

template<class T>
class BASE_Polymorphic {
    friend T; //only the actual type can register this class at the CASTS data structure.

private:
    void Register_Inheritence(const Type *base);
public:
    virtual ~OOP_Polymorphic() {}
    static const Type *Get_Type();
    virtual const Type *My_Type();
};

class CASTS{
private:
    static Class_Hierarchy classHierarchy;
public:
    static void Register_Inheritence(const Type *derived, const Type *base);

    template<typename Dst, typename Src>
    static Dst new_static_cast(Src src);

    template<typename Dst, typename Src>
    static Dst new_dynamic_cast(Src src);

};

#include "a.cpp"
#endif /* A_H_ */

and in a.cpp file:

#ifndef A_CPP_
#define A_CPP_

#include "hw5.h"
#include <type_traits>
#include <typeinfo>

inline void CASTS::Register_Inheritence(const Type *derived, const Type *base) {
    std::map<const Type*, std::set<const Type*> >::iterator it;
    it = CASTS::classHierarchy.map.find(derived);
}

#endif /* A_CPP_ */

I have another file called test.cpp which #includes a.h and uses it's code.

At first I had map as a static private data member in CASTS and had it's initialization in a.cpp, but I had undefined reference to BASE_Polymorphic member functions, so I included a.cpp in a.h.

Then I had another problem, I couldn't initialize map (private static data member of CASTS class) in the cpp file because I started getting multiple definition error. Therefore I tried making Class_Hierarchy which has map as a public data member and make a static instance of Class_Hierarchy in CASTS class to use its map, but I'm getting undefined reference to CASTS::classHierarchy.

I can't think of a solution for both problems, How can I keep the template class in the header file and have a static member in CASTS class?

Right now the only error I have is undefined reference toCASTS::classHierarchy'`.

EDIT:

I had an error of multiple definition for every member function, so I added #include guards on a.cpp file.

Loay
  • 483
  • 3
  • 18
  • 1
    You're probably missing include guards in your header. – AndyG Jun 21 '16 at 19:28
  • Try adding `#pragma once` at the top of your header file – KABoissonneault Jun 21 '16 at 19:29
  • I have include guards in header file – Loay Jun 21 '16 at 19:57
  • 1
    You shouldn't include the cpp file in the header file, include headers in cpp files, compile cpp files. Templates all in headers and static variables need definitions (the one in the class is just a declaration) in cpp files. I've glossed over the detail, but that is the usual working approach (and works when learning the language). – Niall Jun 21 '16 at 20:04
  • See here; http://stackoverflow.com/q/9282354/3747990 – Niall Jun 21 '16 at 20:07
  • Closed as duplicate on the more popular post, but the other link above would also be a good duplicate. – Niall Jun 21 '16 at 20:11
  • @Niall I had to include the cpp in the header because I had linking error for the template class. I found this solution from here: http://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp – Loay Jun 21 '16 at 20:12
  • @Niall if I define the static member in the cpp I get mutiple definition error – Loay Jun 21 '16 at 20:14
  • and if I remove the include of the cpp from the header I get undefined reference error (for the template class) – Loay Jun 21 '16 at 20:15
  • Just put all the template code in the header and the static definition in the cpp file, including a cpp is a bad idea. http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Niall Jun 21 '16 at 20:15
  • *if I define the static member in the cpp I get mutiple definition error* - that's one reason to not include the cpp file in the header. – Niall Jun 21 '16 at 20:17
  • If the template code must be in a seperate file, you can use a file extension something like tpp or inl, but generally the code is included in the header file proper (sometimes inline after the class definition). – Niall Jun 21 '16 at 20:23
  • What about template functions in a non-template class ? should they be implemented in header too ? – Loay Jun 21 '16 at 20:42
  • @Niall: OP is actually using a common pattern.. except people typically use ".hpp" or ".cxx" or something to indicate that it's not really a .cpp file proper. OP: How are you compiling? Make sure you aren't including the .cpp file as part of the project. – AndyG Jun 21 '16 at 21:23
  • @AndyG noted; http://stackoverflow.com/questions/37952954/private-static-data-member-template-class?noredirect=1#comment63356252_37952954 I've seen more .inl than the others, but the extension is almost never cpp. The post includes a header `hw5.h` that looks like homework, I've tried to tailor the recommendations for a more general approach. – Niall Jun 21 '16 at 21:29

0 Answers0