为什么我收到此错误?
从std :: string到std :: string的无套件转换函数是否存在? 在memcpy(值,(字符串*)(地址),sizeof(值))中的','处出现错误;
int main ()
{
string adress;
cout << "Please, enter the adress you want to access: " << endl << endl;
getline (cin,adress);
cout << "The adress is : " << adress << endl << endl;
getchar();
string value[512];
memcpy (value, (string*)(adress), sizeof(value));
cout << "The value of " << adress << " is: " << adress;
getchar();
return 0;
}
答案 0 :(得分:1)
memcpy期待一个地址,你应该更改它memcpy(值,(字符串*)(&amp;地址),sizeof(值))以避免警告。
但是,std :: string是一个模板类,使用memcpy是不安全的,如果要复制它,只需执行:
string newString = address;
如果要复制到char缓冲区,请执行以下操作:
char buffer[255] ;
strcopy(buffer, address.c_str())
答案 1 :(得分:0)
您的代码:
string value[512];
memcpy (value, (string*)(adress), sizeof(value));
似乎在说它想要这样做:
string value[512];
for( int i=0; i<512; ++i )
value[i] = adress;
而您输出给用户:
cout << "Please, enter the adress you want to access: " << endl << endl;
似乎想做更像这样的事情:
long lTemp = atol(adress.c_str());
void *pAddress = (void*)lTemp;
lTemp = *((char*)pAddress);
cout << "Found: " << lTemp << " in adress" << endl;
编辑:在睡觉之后,也许你的意思是这样:
char value[512];
memcpy (value, (void*)atol(adress.c_str()), sizeof(value));
cout << "Read: ";
for( int i=0; i<sizeof(value); ++i ) printf("0x%x ", (int)value[i]);
cout << endl << endl;
哪个呃,不可取。 (更不用说32对64位指针转换问题)