将对象成员传递给期望整数的模板函数

时间:2022-12-05 16:11:43

I have been reading and reading posts asking similar questions but my doubts persists.

我一直在阅读和阅读提出类似问题的帖子,但我的怀疑仍然存在。

So I have a class like this:

所以我有一个这样的课:

class Instruction{
public:
    unsigned int getAddress();
    uint32_t getValue();
private:
    unsigned int address;
    uint32_t value;
}


Then I need to convert a decimal to hexadecimal an write it to a string. I saw this question so I took the function in the answer and put it my Utils.hpp class like this:

然后我需要将十进制转换为十六进制,并将其写入字符串。我看到了这个问题所以我在答案中使用了函数并将它放在我的Utils.hpp类中,如下所示:

Utils.hpp

class Utils{
public:
    ...
    static template< typename T > static std::string toHex( T &i );
}


Utils.cpp

template< typename T >
std::string toHex( T &i ){
    std::stringstream stream;
    stream  << "0x" << std::setfill ('0') << std::setw(sizeof(T)*2)
            << std::hex << i;
    return stream.str();
}
std::string Utils::toHex<unsigned int>();
std::string Utils::toHex<uint32_t>();


On main I have this:

在主要我有这个:

std::stringstream stream;

Instruction *newInstruction = new Instruction(addr, inst); // this attributes the parameters

stream  << Utils::toHex(newInstruction->getAddress()) << " "
        << Utils::toHex(newInstruction->getValue()) << endl;

And I get the following compiling errors:

我得到以下编译错误:

main.cpp: In function 'int main(int, char**)':
main.cpp:39: error: no matching function for call to 'Utils::toHex(unsigned int)'
Utils.hpp:16: note: candidates are: static std::string Utils::toHex(T&) [with T = unsigned int]
main.cpp:41: error: no matching function for call to Utils::toHex(uint32_t)'
Utils.hpp:16: note: candidates are: static std::string Utils::toHex(T&) [with T = unsigned int]
make: *** [main.o] Error 1

I really need help to figure out how I can make this work, because I am relatively new to this stuff.

我真的需要帮助来弄清楚我是如何做到这一点的,因为我对这些东西比较陌生。


Thank you in advance!

先感谢您!

2 个解决方案

#1


4  

You should make toHex accept const reference to T: toHex(const T&). Otherwise you cannot pass a temporary to it, whereas the result of function call is a temporary.

你应该使toHex接受对T:toHex(const T&)的const引用。否则你不能传递临时值,而函数调用的结果是临时的。

Also note that the question/answer you are referring to is not working with references at all, it has

另请注意,您所指的问题/答案根本不适用于参考文献

std::string int_to_hex( T i )

#2


1  

You are missing Utils:: prefix in the definition of the function and, as said earlier, const in the reference. The corrected Utils.cpp should be:

你在函数的定义中缺少Utils :: prefix,如前所述,在引用中是const。更正后的Utils.cpp应为:

template< typename T >
std::string Utils::toHex(const T &i ){
    std::stringstream stream;
    stream  << "0x" << std::setfill ('0') << std::setw(sizeof(T)*2)
            << std::hex << i;
    return stream.str();
}

#1


4  

You should make toHex accept const reference to T: toHex(const T&). Otherwise you cannot pass a temporary to it, whereas the result of function call is a temporary.

你应该使toHex接受对T:toHex(const T&)的const引用。否则你不能传递临时值,而函数调用的结果是临时的。

Also note that the question/answer you are referring to is not working with references at all, it has

另请注意,您所指的问题/答案根本不适用于参考文献

std::string int_to_hex( T i )

#2


1  

You are missing Utils:: prefix in the definition of the function and, as said earlier, const in the reference. The corrected Utils.cpp should be:

你在函数的定义中缺少Utils :: prefix,如前所述,在引用中是const。更正后的Utils.cpp应为:

template< typename T >
std::string Utils::toHex(const T &i ){
    std::stringstream stream;
    stream  << "0x" << std::setfill ('0') << std::setw(sizeof(T)*2)
            << std::hex << i;
    return stream.str();
}