tags:

views:

764

answers:

9

This line:

strcat(query,*it);

(where *it is an iterator to a string)

Keeps giving me this error:

no matching function for call to \`strcat(char[200], const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'

I guess it's because strcat takes in a char* while *it is a string. How do I convert it from a string to a char* to make it work with strcat() ?

I've tried strcat(query,(*it).c_str()) but that just gives me a runtime error.

Edit: sorry, it should be converted to a const char*

+7  A: 

strcat(query,(*it).c_str()) should work. What's the runtime error? Are you sure that query is null-terminated before you make the call?

tragomaskhalos
Yup, I am sure query is null-terminated.I also tried it with printf("%s", (*it).c_str() );
Steve
@Steve: How are you initializing the iterator?
Michael Burr
+1  A: 

You need to be more specific about which runtime error you get. Calling c_str() on the string should be the correct solution. As always with strcat() and other classic C string functions without bounds checking, you must be careful not to pass it too long of an input.

unwind
Too long input is no problem. If it fits in your adress space, strcat should work. What _is_ a problem, is a string with no terminating '\0'.
gnud
Unfortunately, I am not sure what the runtime is. I am using Dev-c++ to compile and the runtime is "mysql.exe has encountered an error and needs to close"
Steve
@gnud: Huh? If the first argument here is char query[200], and the std::string is 2000 characters long, then surely that is a problem?
unwind
A: 

Try this (I assume that the runtime error is because of a NULL/invalid pointer):

for (...; it != str.end(); ++it)
...
   if (!it->empty())
   {
      strcat(query, it->c_str());
   }

EDIT: Sorry, c_str() never returns NULL, which I temporarily forgot, so it is always safe. Unless the query buffer is not long enough to be able to contain all of the concatenated strings of course (or there is some other issue, like iterator beyond .end(), the container modified during the loop, or something similar).

Stefan Rådström
c_str() does not return a null pointer, even if the string is empty.
Chris Jester-Young
Oops, was a bit too fast there... Another possible cause in the line of this is however that the iterator is not checked for .end() (or a multithreading/reentrancy error)
Stefan Rådström
+2  A: 

Buffer overflow?

char   query[200] = {0}; // Make sure this array initialized before
                         // you start concatenating strings onto it.

for (it = vec.begin();it != vec.end();++it)
{
   if ((strlen(query) + it->length() + 1) >= 200)
   {
       logError("Buffer oveflow detected.";
       break;
   }
   strcat(query, it->c_str());
}
Martin York
+1  A: 

When you're sure about the length of the buffer you're strcatting in (e.g. 200), better use strncat; this will rule out the Buffer Overflow mentioned by @Martin. Otherwies check for the total length before concatenating (this is a precondition for its use!)

Queries typically become way longer than 200 characters, by the way. If you're not sure about the length of the resulting query, fall back on a dynamic string, like std::string.

xtofl
A: 

Is *it pointing at a valid string in all cases? Could it be pointing at end() in the last iteration? Or may be the container it points into got modified, invalidating *it.

Arkadiy
A: 

If the application is in release mode, trace the application by putting the message boxes or by generating the interrupt 3. (_asm int 3;) in particular places. And if you have put the interrupt it exe will popup a debug message. Attach the process to Visual Studio to debug it. Hope this way we can know the place of crash.

Vinay
+1  A: 

Use the debugger, Luke!

(*it).c_str() Sure as hell should be a valid argument for strcat, assuming that your iterator is valid, and assuming that query is a null-terminated string, so should that. The quickest way to find out which of them are misbehaving is to watch it do so and inspect the values of it and query at runtime.

korona
+1  A: 

Since you've ruled out that query is not null-terminated, it seems the consensus is that the problem is likely to be one of the following:

  1. buffer overflow - the buffer pointed to by query is not large enough to have (*it).c_str() concatenated to it
  2. the itereator, it, is invalid. This can happen in several ways, including:

    • not being properly initialized;
    • has the value of someContainer.end();
    • or the container has been modified in some way that invalidates an existing iterator

You should be able to determine what's going on with a debugger. Also, I'm sure if you post more code, that shows how query and it are defined and used, you'll get a definitive answer here, too (how's that for remote debugging).

Michael Burr