In C# I can easily enumerate through an array of strings. However, why doesn't the code below work in C++?
int main(int argc, char ** argv)
{
string hi[] = { "hi", "cool", "what" };
for (string s : hi)
{
printf("%s \n", s);
}
return 0;
}
Also, I tried using this instead, but it doesn't work either:
printf(s);
Oddly enough, an array on integers does work, but using %d
. And yes, I do have #include <string>
.
Error information provided by @chris (generated by the Clang compiler):
main.cpp:12:25: error: cannot pass non-trivial object of type 'string' (aka
'basic_string<char, char_traits<char>, allocator<char> >'
) to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs]printf("%s \n", s); ~~ ^
main.cpp:12:25: note: did you mean to call the c_str() method?
printf("%s \n", s); ^ .c_str()