printf()令人惊讶的控制台输出

Joc*_*hen 1 c++ printf

经过几年的休整,我需要用C++开发.无论如何,我在阅读我正在阅读的教程后遇到了问题.在编写下面的代码片段后,我希望在我的控制台中看到"Hello World",但我只能看到'Debug:StrangeChars'; 什么地方出了错?

std::string myString("Hello World"); 
printf("* Debug: %s \n", myString);
Run Code Online (Sandbox Code Playgroud)

Ser*_* L. 14

printf依靠你传递正确的论点.%s需要一个char *,你通过了std::string.

试试(C路)

char myString[] = "Hello World";
printf("* Debug: %s \n", myString);
Run Code Online (Sandbox Code Playgroud)

或者(混合C/C++方式)

std::string myString("Hello World"); 
printf("* Debug: %s \n", myString.c_str());
Run Code Online (Sandbox Code Playgroud)

或者C++方式:

std::string myString("Hello World"); 
std::cout << "* Debug " << myString << std::endl;
Run Code Online (Sandbox Code Playgroud)