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

时间:2013-06-02 19:36:41

标签: c++ types cout

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

错误:没有运算符“<<”匹配这些操作数,操作数类型是std:ostream<< STD:字符串

#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();
}

1 个答案:

答案 0 :(得分:6)

你忘了:

#include <string>

您不应该依赖相关的标准标题通过包含其他标题来间接包含。

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

int main()
{
    // ...
}