将字符串变量的值与一些文本链接,并将其打印到C++中的标准输出

Chain value of string variable with some text and print it to standard output in C++

本文关键字:打印 C++ 标准输出 文本 变量 字符串 链接      更新时间:2023-10-16

我想做一些非常简单的事情:我有一个有字符串参数的函数,我想把它链接到某个常量字符串,然后把结果输出到控制台,如下所示:

void test(string s){
    cout << "Parameter of this function was: " << s;
}

在其他语言中,像这样的链接是有效的,但在C++中,编译器不满意:error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

您可能忘记了#include <string>#include <iostream>

您使用的Visual Studio是什么版本?您的代码示例是正确的C++(只要您有适当的"using namespace std;"(。

将类似的代码放入g++中效果良好。