插入操作符不使用向量,我不知道为什么

时间:2017-09-02 09:42:38

标签: c++ compiler-errors linker-errors

所以我开始编写我的代码,我将测试是否仍然记得如何投射,直到我的操作符下面出现红线。 这是编译器错误:

Error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion) (12)

老实说,输出一个字符串/向量从来没有问题,所以我不知道如何解决这个问题。有人可以告诉我如何解决这个问题。如果您能告诉我代码有什么问题,那也很棒。

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<string>hello;
    hello.push_back("9");
    for (auto i : hello)
        cout << i << " "; <-- The first operator is underlined. Why?
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您的计划中还需要一个包含内容:

#include <string>

虽然<iostream>确实声明/定义了一些与字符串相关的函数,而不是所有函数。

对于某些编译器,iostream标头在内部包含字符串,但标准并不需要 - 而Visual Studio也没有,这就是您收到此错误的原因。