C++ cout操作数错误,输入错误ostream,string

Sox*_*arm 2 c++ types cout

我只是在学习c ++的基础知识,而且我在使用cout时遇到了一些问题.我写了一些简单的测试来打印hello world,一个简单的加法器函数,以及一个翻转字符串顺序的函数.一切都运行良好,除了我的字符串函数,给出了我的错误.我会喜欢解释,谢谢.

错误:没有运算符"<<"匹配这些操作数,操作数类型是std:ostream << std:string

#include <iostream>

using namespace std;

int adder(int a, int b)
{
    return a + b;
}

int addOneToInput(int a)
{
return a + 1;
}

string flipStringOrder(string s)
{
string temp = "";
for (int i = 0; i < s.length; i ++)
{
    char charTemp = (s.at(s.length() - i -1));
    temp += charTemp;
}
return temp;
}


void main(){
cout << "Hello World" << endl;
int x = 5;
int y = 3;
cout << adder(x, y) << endl;
cout << flipStringOrder("moon") << endl;
cin.get();
}
Run Code Online (Sandbox Code Playgroud)

And*_*owl 6

你忘了:

#include <string>
Run Code Online (Sandbox Code Playgroud)

您不应该通过包含其他标头间接地依赖相关的标准标头.

此外,将签名更改main()为合法签名,例如:

int main()
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)