0

So, I've read a lot online for this error, but for some reason, I'm still getting it even after I have tried the suggested things. If anyone could help me understand this and point out what's wrong, that would be awesome.

char * s = strtok(text, ",");
string name = s;
printf("%s", name);
librik
  • 3,738
  • 1
  • 19
  • 20
  • 3
    `printf()` takes a `char *` for `%s`, not a `std::string`. You can't just ignore that requirement. `printf("%s", name.c_str());` might work for you. – Crowman May 21 '14 at 03:21
  • 1
    Do you need to even be using `printf`? You should be using streams ie. `std::cout << name << std::endl`. – Fantastic Mr Fox May 21 '14 at 03:25
  • 1
    Just putting it out there: http://stackoverflow.com/questions/10865957/c-printf-with-stdstring – chris May 21 '14 at 03:25
  • 1
    I would also suggest tokenising your string differently, again if you are actually using c++: http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – Fantastic Mr Fox May 21 '14 at 03:30

1 Answers1

1

Given your example code the error you get is saying something like you cannot pass a non-POD object to an ellipses. This is because you are trying to pass a non-POD type to a variadic function, one that takes a variable number of arguments. In this case by calling printf which is declared something like the below

int printf ( const char * format, ... );

The ellipsis used as the last parameter allows you to pass 0 or more additional arguments to the function as you are doing in your code. The C++ standard does allow you to pass a non-POD type but compilers are not required to support it. This is covered in part by 5.2.2/7 of the standard.

Passing a potentially-evaluated argument of class type having a non-trivial copy constructor, a non-trivial move contructor, or a non-trivial destructor, with no corresponding parameter, is conditionally-supported with implementation-defined semantics.

This means it is up to each compiler maker to decide if they want to support it and how it will behave. Apparently your compiler does not support this and even if it did I wouldn't recommend using it.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74