【问题标题】:Keep receiving this error main.cpp:9:91: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘const std::vector’继续收到此错误 main.cpp:9:91: error: no match for ‘operator<<’ (操作数类型是 ‘std::basic_ostream’ 和 ‘const std::vector’
【发布时间】:2021-08-23 00:55:45
【问题描述】:

如果时间较长,请提前道歉。问题在第 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;
}

如果可能的话,我们也感谢任何和所有的帮助,您能否解释一下如何避免将来出现这样的错误。 提前致谢。

【问题讨论】:

    标签: c++ vector operator-overloading operators


    【解决方案1】:

    你不能直接打印ratings cout ... &lt;&lt; ratings ... 因为std::vector 没有用于打印的运算符重载。相反,您必须打印出该向量内的一个元素,因此将其更改为 cout ... &lt;&lt; ratings[i] ...,我假设这是您想要的效果。

    这正是编译器错误告诉您的内容。 std::vector 没有过载(没有 operator&lt;&lt; 匹配)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多