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 to
CASTS::classHierarchy'`.
EDIT:
I had an error of multiple definition for every member function, so I added #include guards on a.cpp
file.