简体   繁体   English

继续收到此错误 main.cpp:9:91: error: no match for 'operator<<' (操作数类型是 'std::basic_ostream' 和 'const std::vector'

[英]Keep receiving this error main.cpp:9:91: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘const std::vector’

Sorry in advance if this is lengthy.如果这很长,请提前抱歉。 the problem is within line 9. (where the 2nd cout is.) apparently, but i'm new to this so I can't identify exactly what the issue is.问题出在第 9 行内。(第二个 cout 所在的位置。)显然,但我是新手,所以我无法准确确定问题所在。

#include <iostream>
#include <vector>
using namespace std;


void outputRoster(const vector<int> &jersey, const vector<int> &ratings) {
    cout << "ROSTER" << endl;
    for (int i =1; i < jersey.size(); ++i) {
        cout << "Player " << i << " -- Jersey number: " << jersey.at(i-1) << ", Rating: " << ratings << endl;
    }
    cout << endl;
}
void addPlayer(vector<int> &jersey, vector<int> &ratings) {
    int num;
    cout << "Enter another player's jersey number: ";
    cin >> num;
    jersey.push_back(num);
    cout << "Enter another player's ratings: ";
    cin >> num;
    cout << endl;
    ratings.push_back(num);
}
void removePlayer(vector<int> &jersey, vector<int> &ratings) {
    int num;
    cout << "Enter a jersey number: ";
    cin >> num;
    cout << endl;
    for (int i = 0; i < jersey.size(); ++i){
        if (jersey.at(i)==num){
            jersey.erase(jersey.begin()+i);
            ratings.erase(ratings.begin()+i);
            break;
        }
    }
}
void updatePlayerRating(const vector<int> &jersey, vector<int> &ratings){
    int num;
    cout << "Enter a jersey number: " << endl;
    cin >> num;
    for (int i = 0; i < jersey.size(); ++i){
        if(jersey.at(i) == num){
            cout << "Enter a new rating for player: ";
            cin >> num;
            cout << endl;
            ratings.at(i) = num;
        }
    }
}
void outputPlayersAboveRating(const vector<int> &jersey, const vector<int> &ratings) {
    int num;
    cout << "Enter a rating: ";
    cin >> num;
    cout << endl;
    cout << "ABOVE " << num << endl;
    
    for (int i = 0; i < ratings.size(); ++i){
        if (ratings.at(i) > num) {
            cout << "Player " << i+1 << " -- Jersey number: " << jersey.at(i) << ", Rating: " << ratings.at(i);
        }
    }
    cout << endl;
}


int main() {
    vector<int> jersey;
    vector<int> ratings;
    
    for (int i = 0; i < 5; ++i) {
        int num;
        cout << "Enter player " << i+1 << "'s jersey number:";
        cin >> num;
        jersey.push_back(num);
        cout << "Enter player " << i+1 << "'s ratings:";
        cin >> num;
        ratings.push_back(num);
        cout << endl;
        cout << endl;
    }
    
    outputRoster(jersey, ratings);
    
    char inp;
    while(true) {
        cout << "MENU" << endl;
        cout << "a - Add player" << endl;
        cout << "d - Remove player" << endl;
        cout << "u - Update player rating" << endl;
        cout << "r - Output players above a rating" << endl;
        cout << "o - Output roster" << endl;
        cout << "q - Quit" << endl;
        cout << "Choose an option: ";
        
        cin >> inp;
        cout << endl;
        
        if (inp == 'a') {
            addPlayer(jersey, ratings);
        }
        else if (inp == 'd') {
            removePlayer(jersey, ratings);
        }
        else if (inp == 'u') {
            updatePlayerRating(jersey, ratings);
        }
        else if (inp == 'r') {
            outputPlayersAboveRating(jersey, ratings);
        }
        else if (inp == 'o') {
            outputRoster(jersey, ratings);
        }
        else if (inp == 'q') {
            return 0;
        }
    }
    return 0;
}

Any and all help is appreciated also if possible, could you explain how to avoid an error like this in the future.如果可能的话,任何和所有的帮助都会受到赞赏,您能否解释一下如何避免将来出现这样的错误。 Thanks in advance.提前致谢。

You cannot print ratings directly in cout... << ratings... because std::vector doesn't have an operator overload for printing.您不能直接在cout... << ratings...中打印ratings ,因为std::vector没有用于打印的运算符重载。 Rather, you have to print out an element inside that vector, so change it to cout... << ratings[i]... , which I'm assuming is your desired effect.相反,您必须打印出该向量内的一个元素,因此将其更改为cout... << ratings[i]... ,我假设这是您想要的效果。

This is exactly what the compiler error is telling you.这正是编译器错误告诉您的内容。 std::vector doesn't have an overload (no operator<< match). std::vector没有重载(没有operator<<匹配)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 \\ main.cpp | 103 |错误:“ operator &lt;&lt;”不匹配(操作数类型为“ std :: ostream {aka std :: basic_ostream <char> }”和“人”) - \main.cpp|103|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Person') 运算符重载时出错(错误:“运算符&lt;&lt;”不匹配(操作数类型为“std::basic_ostream”)<char> &#39; 和 &#39;const 字符 [2]&#39;) - Error while operator overloading (error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const char [2]’) 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::basic_ostream”<char> &#39; - error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ 错误:“operator&lt;&lt;”不匹配(操作数类型为“std::ostream {aka std::basic_ostream”<char> }&#39; 和 &#39;void&#39;) - error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’) 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '}</char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char> }'和'列表')</char> - error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List') 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'std::_List_iterator<int> ')</int></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’) 出现错误:'operator&lt;&lt;' 不匹配(操作数类型为 'std::basic_ostream<char> ' 和 'Complex') 尽管重载了 &lt;&lt; 运算符</char> - Getting error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'Complex') despite overloading the << operator 如何修复错误:'operator&lt;&lt;' 不匹配(操作数类型为 'std::ostream {aka std::basic_ostream<char> }' 和 'void')同时使用字符串和堆栈</char> - How to fix error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void') while using strings and stack 'operator&lt;&lt;' 不匹配(操作数类型为 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'const std::type_index')</char> - No match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::type_index’)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM