简体   繁体   中英

C++ Function returns int to string variable

I have a function that generates a random number. The problem is that I use it in a template class where T may be int or string. When I choose string I get an error about impossible conversion. How can I return an integer and assign it to a string depending on the template type?

CTable(){
    for (int i = 0; i < 10; i++){
        int row = GenerateNumber();
        int col = GenerateNumber();
        T value = GenerateNumber(); //problem here when T is string
        CCellDescr c(row, col, value);
        cells.push_back(c);
    }
}

int GenerateNumber(){
    int number = rand() % 10 + 1;
    return number;
}

You could create a helper template to perform the conversions and specialize it on the T type. For example:

Example Code

#include <iostream>
#include <string>

int GenerateNumber()
{
    int number = rand() % 10 + 1;
    return number;
}

template<typename T>
T convert(int value)
{
    return value;
}

template<>
std::string convert(int value)
{
    return std::to_string(value);
}

template<typename T>
class CTable
{
public:
    CTable()
    {
        for (int i = 0; i < 10; i++)
        {
            int row = GenerateNumber();
            int col = GenerateNumber();
            T value = convert<T>(GenerateNumber());
            std::cout << "'" << value << "'\n";
        }
    }
};

int main()
{
    CTable<std::string> table;

    return 0;
}

Example Output

'8'
'6'
'10'
'8'
'4'
'7'
'2'
'10'
'3'
'6'

Live Example

You can also specialize the templated function to handle string differently

#include <cstdlib>
#include <string>

template<class T>
class CTable
{
public:
    CTable();
};

Removed the constructor's definition from the class. Normally I'm down on this because you gain more potential for screw-ups than benefits from separating template definition and implementation, but here it works.

int GenerateNumber()
{
    int number = rand() % 10 + 1;
    return number;
}

Generate number function is unchanged.

std::string randomStrings[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

template<>
CTable<std::string>::CTable()
{
    std::string value = randomStrings[GenerateNumber()]; 
}

Template constructor implementation for string s. This uses the random number as an index to an array of strings. Not super random, but it gets the point across.

template<class T>
CTable<T>::CTable()
{
    T value = GenerateNumber(); 
}

Template constructor implementation for everything else.

class foo
{

};

And a quickie class to demonstrate how to make this solution fall down.

int main()
{
    CTable<int> a;
    CTable<std::string> b;
    CTable<foo> c; // awwww shoot. Need protection from foos too. 
}

And a quickie main to try it all out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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