插入运算符无法使用矢量,我不知道为什么

Insertion operator is not working with vector and I don't know why

本文关键字:我不知道 为什么 运算符 插入      更新时间:2023-10-16

所以我开始写我的代码,然后测试,看看我是否仍然记得如何施放,直到我在操作员下面得到一条红线为止。这是编译器错误:

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;
}

您需要在程序中再包含一个:

#include <string>

虽然<iostream>确实声明/定义了一些字符串相关的功能,而不是全部。

使用一些编译器,iostream标题在内部inclldues字符串,但是标准并不需要 - Visual Studio不需要,这就是为什么您会收到此错误的原因。