我无法在非模板类中链接模板函数 我有:
class base {};
class mgr : base
{
template function a;
........
non template functions
};
模板类m
的访问变量
例如
static m<T>* ptr;
ptr = reinterpret_cast<m<T>*>( std::get<0>(d) );
ptr->m<T>::clear();
(m private section is friend of mgr)
课程g
中的我想使用函数a
static mgr mg;
mgr *p = &mg;
m<T> v = *( reinterpret_cast<T*>( p->a(p1,p2) );
note that func. a() return pointer to base (similar idea to void*) regardless of <T>
在m.cpp
的末尾我有(每个专业)
例如:
template class m<string>;
在mgr.cpp
的末尾我有(每个专业)
例如:
template pbase mgr::a<string>( ... );
用于声明的hpp文件, 用于定义的cpp文件(实现)
观看链接地图我可以看到mgr
链接模板专用
函数a
但它无法在g
任何帮助将不胜感激
这里有更多代码 mgr.hpp
#ifndef __mgr__mgr___
#define __mgr__mgr___
#include <iostream> // cout
#include <ostream> // cout
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <type_traits>
#include <limits>
#include <climits>
#include <exception>
using namespace std;
typedef string tInstName;
enum tInstType { Typ1 , Typ2 , Typ3 };
class mgr
{
public:
class MIObase {};
map<string,tuple<MIObase*,string,tInstType>> Imap;
template <class T> MIObase* getInstance ( const string& name , const iName& instname );
};
#endif
mgr.cpp
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <limits>
#include <climits>
#include <exception>
#include "mgr.hpp"
#include "mfunc.hpp"
using namespace std;
template<>
pMIObase mgr::getInstance<string> ( const string& filename , const string& name )
{
tuple<pMIObase,string,tInstType> d;
static mfunc<string>* ptr;
static pMIObase ptr_;
auto it = Imap.find ( instname );
if ( it != Imap.end() )
{
d=Imap[instname];
ptr = reinterpret_cast<mfunc<string>*>( std::get<0>(d) );
ptr->mfunc<string>::clear();
ptr_ = std::get<0>(d);
cout<<instname <<" already exists .......\n";
return ptr_;
}
else
{
//mfunc<string> I;
ptr = new mfunc<string>;
d = make_tuple (reinterpret_cast<pMIObase>(ptr),name,Tstr);
Imap[instname] = d;
ptr->clear();
ptr_ = reinterpret_cast<pMIObase>(ptr);
return ptr_;
}
}
template pMIObase mgr::getInstance<string>(const string& filename , const tInstName& instname);
g.cpp
#include "mgr.hpp"
using namespace std;
void g()
{
static mgr mb;
mgr *pMIO=&mb;
// this one dose not link
static mfunc<string> &v = *( reinterpret_cast<mfunc<string>*>( pMIO->mgr::getInstance<string> ( ) ));
}