C++ 智能感知:没有运算符“<<”与这些操作数匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28187714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:55:43  来源:igfitidea点击:

IntelliSense: no operator "<<" matches these operands

c++visual-studio

提问by Mohamadreza

I'm getting the error:

我收到错误:

 IntelliSense: no operator "<<" matches these operands
 operand types are: std::ostream << std::string c:\Users\mohammad\Documents\Visual Studio 2013\Projects\summing a list of number\summing a list of number\summing a list of number.cpp  10

Here is the code:

这是代码:

 // summing a list of number.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;
int sum(int a[], int from, int size, const string& context, int depth)
{
    string indent(depth, '|');
    cout << indent << context << "(a, " << from << ", " << size << ")" << endl;
    int result = 0;
    if (size == 1)
    {
        result = a[from];
    }
    else if (size > 1)
    {
        int midpoint = size / 2;
        int left = sum(a, from, midpoint, "left", depth + 1);
        int right = sum(a, from + midpoint, size - midpoint, "right", depth + 1);
        result = left + right;
        cout << indent << "=" << left << "+" << right << endl;
    }
    cout << indent << "=" << result << endl;
    return result;
}

int main(){
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    cout << "sum = " << sum(a, 0, 10, "sum", 0) << endl;
    getchar();
}

Why does it say there is an error due to std and ostream, when I already have included iostream and std?

当我已经包含了 iostream 和 std 时,为什么会说由于 std 和 ostream 而出现错误?

I'm using VS-2013.

我正在使用 VS-2013。

回答by Mohamadreza

by changing this line:

通过改变这一行:

#include "iostream"

into

进入

#include <iostream>

and adding srings as:

并添加 srings 为:

#include <string>

it worked.

有效。

回答by rauzan

Add to your include region a new line with following: #include "string". IntelliSense and even your build system do not know what this string type objects are. You should include the above mentioned header (which is the declaration) for the type string from std to let both know what it is and what you meant.

添加到您的包括地区的新线有以下几点: #include "string"。IntelliSense 甚至您的构建系统都不知道这个字符串类型对象是什么。您应该为 std 中的类型字符串包含上面提到的标头(即声明),以让两者都知道它是什么以及您的意思。

回答by Neo Ravi

you should also include string library like this i.e #include <string>and change from #include "iostream" to #include <iostream>

您还应该包括这样的字符串库,即 #include <string>从 #include "iostream" 更改为#include <iostream>