64,690
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
template<int V>
class CType
{
};
template<>
class CType<1>
{
};
class CMyTmp
{
public:
bool Fun(int nVal)
{
CType<nVal> tmpObj;
return Foo(tmpObj);
}
private:
template<class T>
bool Foo(T& nVal);
bool Foo(CType<1>& nVal);
};
template<class T>
bool CMyTmp::Foo(T& nVal)
{
cout << "General Foo()" << endl;
return true;
}
bool CMyTmp::Foo(CType<1>& nVal)
{
cout << "Dongle WriteSnEx()" << endl;
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
CMyTmp MyObj;
MyObj.Fun(1);
MyObj.Fun(2);
return 0;
}
1>Test.cpp
1>h:\program records\test\test.cpp(23) : error C2971: 'CType' : template parameter 'V' : 'nVal' : a local variable cannot be used as a non-type argument
1> h:\program records\test\test.cpp(10) : see declaration of 'CType'
1> h:\program records\test\test.cpp(21) : see declaration of 'nVal'
template<>
bool Fun<2>();
template<int V> bool Fun();
template<int V>
class CType
{
};
template<>
class CType<1>
{
};
class CMyTmp
{
public:
template<int nVal>bool Fun()
{
CType<nVal> tmpObj;
return Foo(tmpObj);
}
private:
template<class T>
bool Foo(T& nVal);
bool Foo(CType<1>& nVal);
};
template<class T>
bool CMyTmp::Foo(T& nVal)
{
cout << "General Foo()" << endl;
return true;
}
bool CMyTmp::Foo(CType<1>& nVal)
{
cout << "Dongle WriteSnEx()" << endl;
return false;
}
int main()
{
CMyTmp MyObj;
MyObj.Fun<1>();
MyObj.Fun<2>();
system("pause");
return 0;
}