-1

Why am i getting this error?

No suiteble conversion function from std::string to std::string exists ? The error apears at the ',' in memcpy (value, (string*)(adress), sizeof(value));

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;
}
John DOe
  • 180
  • 1
  • 1
  • 12
  • Are you looking for `std::fill_n`? Not that `value` is even used, though. – chris Jun 03 '13 at 19:59
  • 6
    What are you trying to do? I ask, because you _are doing it wrong_. – Chad Jun 03 '13 at 20:00
  • See http://stackoverflow.com/questions/16108162/how-do-i-get-rid-of-the-intellisense-no-suitable-conversion-function-from-std. – kirbyfan64sos Jun 03 '13 at 20:00
  • Trying to get the value from a memory location, and i am not surpriced that i am doing it wrong, very bad with C++, learning slowly ... – John DOe Jun 03 '13 at 20:00
  • You _already_ have the "value" in `address`. What are you attempting to do with `value`? – Chad Jun 03 '13 at 20:01
  • 6
    @JohnDOe you're learning slowly because you're not learning using the right approach - with a book. C++ isn't something you can learn by throwing some code in an IDE and expecting it to work. – Luchian Grigore Jun 03 '13 at 20:01
  • 1
    @JohnDOe, [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – chris Jun 03 '13 at 20:03
  • 2
    Do not use `memcpy` to copy non-POD objects. It can lead undefined behavior and usually does. – Captain Obvlious Jun 03 '13 at 20:03
  • See http://stackoverflow.com/questions/12298208/c-get-value-of-a-particular-memory-address – Sceptical Jule Jun 03 '13 at 20:13
  • 1
    Why don't you show us (1) the *exact* error message, and (2) code which reproduces the problem? Trying to compile your code I get a whole string of *other* errors, because of all the bits you cut out – jalf Jun 03 '13 at 20:35

2 Answers2

1

memcpy is expecting an address, you should change it memcpy (value, (string*)(&adress), sizeof(value)) to avoid the warning.

however, std::string is a template class, it is not safe to use memcpy, if you want to copy it, just do:

string newString = address;

if you want to copy to a char buffer do:

char buffer[255] ;
strcopy(buffer, address.c_str())
  • Is it unsafe because string is a template? Is it even a template (I was unaware you can do `std::string` ? Or a specialization of another template? – Luchian Grigore Jun 03 '13 at 20:15
  • I have to agree with @LuchianGrigore, the reason it's unsafe has nothing to do with it being a template. – Mark Ransom Jun 03 '13 at 20:18
  • what I tried to say was that string is not a type like int or char, it is actually a template type, "typedef basic_string string", and memcpy should be used only with blocks of data. – Ildefonso RM Jun 03 '13 at 20:27
0

Your code:

string value[512];
memcpy (value, (string*)(adress), sizeof(value));

seems to be saying it wants to do this:

string value[512];
for( int i=0; i<512; ++i )
    value[i] = adress;

Whereas your output to the user:

cout << "Please, enter the adress you want to access: " << endl << endl;

seems to want to do something more like this:

long lTemp = atol(adress.c_str());
void *pAddress = (void*)lTemp;
lTemp = *((char*)pAddress);
cout << "Found: " << lTemp << " in adress" << endl;

EDIT: After sleeping on it, perhaps you meant this:

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;

Which uh, isn't advisable. (not to mention 32 vs 64 bit pointer conversion issues)