我在Visual Studio 2013中使用C ++ 11.以下是我的上下文:我想调用模板方法:
template<typename T> bool InvokeMyMethod(std::string methodName, T &retValue, int modeType...)
并传递template参数作为返回值。这样,我会使用integer,float,std::string
参数调用此函数。
但是,当我尝试将std::string
分配给模板参数时,我收到错误。这是我的标题和实现代码:
// include files ...
int IncValue(int iParam)
{
return ++iParam;
}
float DivValue(float fParam)
{
return (fParam / 2);
}
const char* GetHello()
{
return "Hello\n";
}
template<typename T> bool InvokeMyMethod(std::string methodName, T &retValue, int modeType...)
{
bool bRet = false;
switch (modeType)
{
case 0: // basic
{
std::cout << " Method " << methodName << " correctly invoked ." << std::endl;
bRet = true;
}
break;
case 1: //call int function
{
va_list args;
va_start(args, modeType);
int iParam = va_arg(args, int);
int result = IncValue(iParam);
va_end(args);
retValue = result;
bRet = true;
}
break;
case 2: // call float function
{
va_list args;
va_start(args, modeType);
float fParam = (float)va_arg(args, double);
float result = DivValue(fParam);
va_end(args);
retValue = result;
bRet = true;
}
break;
case 3: //call string function
{
const char *strReturn = GetHello();
std::string sValReturned;
sValReturned = strReturn;
std::cout << " Template for string : " << typeid(T).name() << std::endl;
//HERE IS MY PROBLEM AT COMPILE TIME
retValue = sValReturned; // if i comment this line, everything is
// ok, otherwise i got compiling error
// like: error C2440: '=' : cannot convert
// from 'std::string' to 'int'
// AND
// cannot convert from 'std::string' to
// 'float'
bRet = true;
}
break;
}
return bRet;
}
// **************** CPP:MAIN *****************
// include files ...
int main()
{
std::string strMethod = "";
strMethod = "Method_A";
int iResult = 0;
int iParam = 10;
if (InvokeMyMethod(strMethod, iResult, 1,iParam))
{
std::cout << " Invoke Method " << strMethod << " returns : " << iResult << std::endl;
}
else
{
std::cout << " Error : Invoke Method " << strMethod << ". Method or Class not found!" << std::endl;
}
strMethod = "Method_B";
float fResult = 0;
float fParam = 9.0f;
if (InvokeMyMethod(strMethod, fResult, 2, fParam))
{
std::cout << " Invoke Method " << strMethod << " returns : " << fResult << std::endl;
}
else
{
std::cout << " Error : Invoke Method " << strMethod << ". Method or Class not found!" << std::endl;
}
strMethod = "Method_C";
std::string sResult ="";
std::string sParam = "Hello";
if (InvokeMyMethod(strMethod, sResult, 3, sParam))
{
std::cout << " Invoke Method " << strMethod << " returns : " << sResult << std::endl;
}
else
{
std::cout << " Error : Invoke Method " << strMethod << ". Method or Class not found!" << std::endl;
}
std::cout << "Press any key...";
std::cin.get();
return 0;
}
当我尝试将(在.h文件中,在模板方法中)字符串值分配给模板参数时,我收到以下错误。
错误C2440:'=':无法从'std :: string'转换为'int'
无法从'std :: string'转换为'float'
我如何解决这个问题?
答案 0 :(得分:0)
尝试以这种方式使用模板专业化:
template<typename T>
bool InvokeMyMethod(std::string methodName, T &retValue, int modeType...)
{
bool bRet = false;
switch (modeType)
{
case 0: // basic
{
std::cout << " Method " << methodName << " correctly invoked ." << std::endl;
bRet = true;
}
break;
case 1: //call int function
{
va_list args;
va_start(args, modeType);
int iParam = va_arg(args, int);
int result = IncValue(iParam);
va_end(args);
retValue = result;
bRet = true;
}
break;
case 2: // call float function
{
va_list args;
va_start(args, modeType);
float fParam = (float)va_arg(args, double);
float result = DivValue(fParam);
va_end(args);
retValue = result;
bRet = true;
}
break;
}
return bRet;
}
template<>
bool InvokeMyMethod(std::string methodName, std::string &retValue, int modeType...) {
bool bRet = false;
const char *strReturn = GetHello();
std::string sValReturned;
sValReturned = strReturn;
retValue = sValReturned;
bRet = true;
return bRet;
}