H
henkoo
i want to explicit instantiate a member function of a class template,
and got some error,
can anyone give some advice to correct it?
3x
complier: g++ 3.2
#include <iostream>
#include <string>
using namespace std;
template <class BT> class FilterRangeItem {
public:
bool m_bIsSetLow;
bool m_bIsSetHigh;
bool m_bIsFailed;
BT m_low;
BT m_high;
FilterRangeItem();
virtual BT _Convert(string);
virtual bool SetFilter(string,string);
};
template <class BT>
FilterRangeItem<BT>::FilterRangeItem() {
m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
}
template <class BT>
BT FilterRangeItem<BT>::_Convert(string str) {
BT a;
cout <<"FilterRAngeItem<BT>::_Convert()"<<endl;
return a;
}
template <class BT>
bool FilterRangeItem<BT>::SetFilter(string low, string high) {
if (low.length()>0) {
m_low = _Convert(low);
if (!m_bIsFailed) m_bIsSetLow = true;
}
if (high.length() > 0) {
m_high = _Convert(high);
if (!m_bIsFailed) m_bIsSetHigh = true;
}
cout <<"SetFilter finished"<<endl;
return m_bIsSetFailed;
}
template int FilterRangeItem<int>::_Convert(string str) {
cout <<"FilterRangeItem<int>::_Convert"<<endl;
return 0;
}
template double FilterRangeItem<double>::_Convert(string str) {
cout <<"FilterRangeItem<double>::_Convert"<<endl;
return 0.0
}
class Filter {
public:
FilterRangeItem<int> iFlt;
FilterRangeItem<double> dFlt;
};
int main(int argc, char* argv[])
{
{
Filter f;
iFlt.SetFilter("3", "5");
dFlt.SetFilter("6.0", "9.10");
}
int x;
cin>>x;
return 0;
}
compiler error:
t1.cpp:43: parse error before `{' token
t1.cpp:47: parse error before `{' token
t1.cpp: In function `int main(int, char**)':
t1.cpp:62: `iFlt' undeclared (first use this function)
t1.cpp:62: (Each undeclared identifier is reported only once for each
function
it appears in.)
t1.cpp:63: `dFlt' undeclared (first use this function)
and got some error,
can anyone give some advice to correct it?
3x
complier: g++ 3.2
#include <iostream>
#include <string>
using namespace std;
template <class BT> class FilterRangeItem {
public:
bool m_bIsSetLow;
bool m_bIsSetHigh;
bool m_bIsFailed;
BT m_low;
BT m_high;
FilterRangeItem();
virtual BT _Convert(string);
virtual bool SetFilter(string,string);
};
template <class BT>
FilterRangeItem<BT>::FilterRangeItem() {
m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
}
template <class BT>
BT FilterRangeItem<BT>::_Convert(string str) {
BT a;
cout <<"FilterRAngeItem<BT>::_Convert()"<<endl;
return a;
}
template <class BT>
bool FilterRangeItem<BT>::SetFilter(string low, string high) {
if (low.length()>0) {
m_low = _Convert(low);
if (!m_bIsFailed) m_bIsSetLow = true;
}
if (high.length() > 0) {
m_high = _Convert(high);
if (!m_bIsFailed) m_bIsSetHigh = true;
}
cout <<"SetFilter finished"<<endl;
return m_bIsSetFailed;
}
template int FilterRangeItem<int>::_Convert(string str) {
cout <<"FilterRangeItem<int>::_Convert"<<endl;
return 0;
}
template double FilterRangeItem<double>::_Convert(string str) {
cout <<"FilterRangeItem<double>::_Convert"<<endl;
return 0.0
}
class Filter {
public:
FilterRangeItem<int> iFlt;
FilterRangeItem<double> dFlt;
};
int main(int argc, char* argv[])
{
{
Filter f;
iFlt.SetFilter("3", "5");
dFlt.SetFilter("6.0", "9.10");
}
int x;
cin>>x;
return 0;
}
compiler error:
t1.cpp:43: parse error before `{' token
t1.cpp:47: parse error before `{' token
t1.cpp: In function `int main(int, char**)':
t1.cpp:62: `iFlt' undeclared (first use this function)
t1.cpp:62: (Each undeclared identifier is reported only once for each
function
it appears in.)
t1.cpp:63: `dFlt' undeclared (first use this function)