I have this template function and class in mc.h/mc.cpp files:
template <class MemT>
class MC;
template <class MemT>
MemT & getMCObj(int t_instNum);
class HBM;
template <class MemT>
class MC
{
public:
MC(){}
friend MemT & getMCObj<MemT>(int t_instNum);
private:
MemT memTobjList[12];
};
class HBM
{
public:
bool someFunc();
private:
int num_inst;
};
In the cpp file I have
template<class MemT>
MemT & getMCObj(int t_instNum)
{
return MC<MemT>::memTobjList[t_instNum];
}
bool HBM::someFunc()
{
num_inst = 1;
}
template class MC<HBM>;
template HBM & getMCObj<HBM>(int t_instNum);
In main:
int main()
{
MC<HBM> mC;
HBM & ptr = getMCObj<HBM>(3);
ptr.someFunc();
return 0;
}
I get this error when I compile:
In file included from mc.cpp:1:0:
mc.cpp: In instantiation of 'MemT& getMCObj(int) [with MemT = HBM]':
mc.cpp:16:43: required from here
mc.h:18:24: error: invalid use of non-static data member 'MC<HBM>::memTobjList'
The first issue was that the I had not explicitly instantiated the template object. I fixed that by adding that to the cpp file. But now there is an error, when I use a friend function getMCObj to access class data member memTobjList. Please help me understand what the issue is here.
Thanks.