0

我正在尝试使用以下代码将指针转换为字符串,并将字符串转换回指针(词法转换)。从指针到字符串的转换工作正常,但反之则不行。

为什么会这样?还有其他方法可以将字符串转换为指针吗?

我不担心字符串格式不正确导致的错误。我试图转换为指针的字符串将始终通过将指针转换为字符串来生成。

这是代码:

//TB_ConvertToString.cpp

#include<iostream>
#include<cstdio>
#include<sstream>
#include<string>

//Functions to convert data types to and from strings   
template <typename T> std::string ToString ( T Number )
{
    std::stringstream ss;
    ss << Number;
    return ss.str();
};

template <typename T> 
T FromString ( const std::string &Text )
{
    std::stringstream ss(Text);
    T result;
    ss >> result;
    return result;
}
//-------------------------------------------------


using namespace std;

int main()
{
    int a =10;  
    int* p1 = &a;
    // this works------------
    string s1 = ToString<int *>(p1);
    printf("\n ptr: %s",s1.c_str());
    //----------------------

    //this gives compilation errors -----
    //int * p2 = FromString<int *>(s1);
    //printf("\n ptr: %p",p2);
    //----------------------


    cout<<"\n\n";
    return 0;
};

取消注释问题部分会出现以下编译错误:

nehak@Marvin:~/Desktop$ g++ TB_ConvertToString.cpp 
TB_ConvertToString.cpp: In function ‘T FromString(const string&) [with T = int*, std::string = std::basic_string<char>]’:
TB_ConvertToString.cpp:39:33:   instantiated from here
TB_ConvertToString.cpp:21:2: error: no match for ‘operator>>’ in ‘ss >> result’
TB_ConvertToString.cpp:21:2: note: candidates are:
/usr/include/c++/4.6/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:122:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}’
/usr/include/c++/4.6/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/include/c++/4.6/istream:126:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.6/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:133:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.6/istream:169:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:169:7: note:   no known conversion for argument 1 from ‘int*’ to ‘bool&’
/usr/include/c++/4.6/istream:173:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/istream:173:7: note:   no known conversion for argument 1 from ‘int*’ to ‘short int&’
/usr/include/c++/4.6/istream:176:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:176:7: note:   no known conversion for argument 1 from ‘int*’ to ‘short unsigned int&’
/usr/include/c++/4.6/istream:180:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/istream:180:7: note:   no known conversion for argument 1 from ‘int*’ to ‘int&’
/usr/include/c++/4.6/istream:183:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:183:7: note:   no known conversion for argument 1 from ‘int*’ to ‘unsigned int&’
/usr/include/c++/4.6/istream:187:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:187:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long int&’
/usr/include/c++/4.6/istream:191:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:191:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long unsigned int&’
/usr/include/c++/4.6/istream:196:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:196:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long long int&’
/usr/include/c++/4.6/istream:200:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:200:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long long unsigned int&’
/usr/include/c++/4.6/istream:205:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:205:7: note:   no known conversion for argument 1 from ‘int*’ to ‘float&’
/usr/include/c++/4.6/istream:209:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:209:7: note:   no known conversion for argument 1 from ‘int*’ to ‘double&’
/usr/include/c++/4.6/istream:213:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:213:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long double&’
/usr/include/c++/4.6/istream:217:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:217:7: note:   no known conversion for argument 1 from ‘int*’ to ‘void*&’
/usr/include/c++/4.6/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/usr/include/c++/4.6/istream:241:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
/usr/include/c++/4.6/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
/usr/include/c++/4.6/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
/usr/include/c++/4.6/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
/usr/include/c++/4.6/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
/usr/include/c++/4.6/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
/usr/include/c++/4.6/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
nehak@Marvin:~/Desktop$ 
4

4 回答 4

4

您的问题实际上可以缩小到这个简单的测试用例:

std::stringstream ss("0xbf845748");
int* p2;
ss >> p2;

这不起作用,因为没有这样的>>运算符重载会初始化您的指针。为了使指针的这种提取工作,您可以使用void*或者甚至可以提取整数,它足够大以保存该地址,然后使用reinterpret_cast<T>使您的指针指向该地址,只是要小心不要以char*重载结束因为这可能会导致你现在正在处理的更糟糕的问题。

请注意,尝试使用存储在其中的地址来初始化指针std::string是非常糟糕的主意。在你决定让它工作之前,确保它真的是你想要的。很可能有更合理的方法来避免这种情况。

于 2013-03-18T09:59:01.443 回答
2

问题是任何类型的指针都可以隐式转换为void*,但void*不能转换为任何类型的指针。在您的情况下,您应该FromString为所有指针专门化功能。像这样

template <typename T> 
T FromString ( const std::string &Text,
    std::enable_if< ! std::is_pointer< T >::value >::type * = nullptr )
{
    std::stringstream ss(Text);
    T result;
    ss >> result;
    return result;
}

template <typename T> 
T FromString ( const std::string &Text,
    std::enable_if< std::is_pointer< T >::value >::type * = nullptr )
{
    std::stringstream ss(Text);
    void * result;
    ss >> result;
    return (T)result;
}
于 2013-03-18T10:08:25.857 回答
2

@LihO,谢谢!这样可行。我不得不在提取指针时使用 std::hex 。

    string s1 = "0x7fff3e8aee1c";
    stringstream ss;
    ss<<s1;
    long long unsigned int i;
    ss>>std::hex>>i;
    int * i_ptr=reinterpret_cast<int *>(i);
于 2013-03-18T10:17:46.033 回答
1

我最终使用了 std::stoul。

void *p = std::stoul(str.c_str(), 0, 16);
于 2019-03-11T19:11:30.777 回答