繁体   English   中英

将 std::string 分配给模板参数期间编译时出错

[英]Error at compile time during assignment of a std::string to template parameter

我在 Visual Studio 2013 中使用 C++ 11。这是我的上下文:我想调用一个模板方法:

template<typename T> bool InvokeMyMethod(std::string methodName, T &retValue, int  modeType...)

并将模板参数作为返回值传递。 这样,我将使用整数、浮点数、 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 : 主要 *****************

// 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”

我该如何解决这个问题?

尝试以这种方式使用模板特化:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM