Search code examples
c++conditional-statementscoutstdstring

std::cout doen't like std::endl and string in conditional-if


main.cpp: In function ‘void PrintVector(std::vector<std::__cxx11::basic_string<char> >&, bool)’:
main.cpp:16:41: error: overloaded function with no contextual type information
  std::cout << ((newline)? (std::endl) : "");
                                         ^~

Why std::cout doen't like std::endl and string in conditional-if?


Solution

  • I changed it to:

    std::cout << (newline? "\n" : "") << std::flush;
    

    It is not possible to write it with ' (would be faster):

    std::cout << (newline? '\n' : '') << std::flush;
    

    because '' is empty and leads to "error: empty character constant".

    The solution with the conditional-if is so complex that one should prefer the following:

    if (newline) std::cout << std::endl;