因此,我开始编写代码,并且要进行测试以查看是否还记得如何进行强制转换,直到运算符(operator)下出现红线为止。
这是编译器错误:

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)

老实说,我从来没有输出字符串/ vector 的问题,所以我不知道如何解决这个问题。有人可以告诉我如何解决此问题。如果您能告诉我代码有什么问题,那也将很棒。
#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;
}

最佳答案

您的程序中还需要包含一个:

#include <string>

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

对于某些编译器,iostream header 在内部包含字符串,但这不是标准所必需的-而Visual Studio则不是,这就是收到此错误的原因。

关于c++ - 插入运算符无法使用 vector ,我也不知道为什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46012464/

10-11 23:02
查看更多