简体   繁体   中英

Error printing a vector with names

I have a vector named "list" and I want to print the names that are in the list but it says

operator "<<" matches these operand

operand types are: std::ostream << std::string

Why do I have this error and how cand I fix it?

#include<iostream>
#include<vector>
#include<algorithm>


using namespace std;
vector<string> list;

vector<string> getAll() {     //I tried to use this function instread of "list"(list is used below) but it's the same result.
    return list;
}


int main() {

    list.push_back("Ion");
    list.push_back("Gabi");
    list.push_back("Sabrina");

    for (string i : list) 
        std::cout << i;           //HERE

    }

You did not #include <string> . It is important.

Yes, I know you were able to instantiate the vector without it. No, that doesn't make a difference. It's pure chance, down to how your standard library implementation is laid out.

The function responsible for making << work on strings is inside that header.

You have missed to add library for string, include it, by #include <string> . The header <string> also declares the stream inserter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM