首页 > 解决方案 > Vector of Objects - C++. No operator "<<" matches these operands, Error

问题描述

I am a beginner in programming.I have a problem. I am trying to code the Enigma machine. I have two classes. One for Enigma, one for rotors. Rotors are small parts of the enigma machine, that doesn't matter for the problem. My problem is the error. I cannot cout, the function cout << rotors[0].GetRotor(); which should return my vector of integers. I have no idea why is that. I don't need that to my program, but I'm not sure if my adding rotor to enigma void AddRotor(Rotor rotor) { rotors.push_back(rotor); }function, called in "TakeRotors" function, works right. In my opinion, it should work well, but I can't check it. Debugger, unfortunately, doesn't show any values of vector<Rotor> rotors; permutation so I am not sure :( Any help would be great. Thank You. Here's my full, needed code :)

#include <iostream>
#include <vector>

using namespace std;

class Rotor {
public:
    vector <int> permutation;
    int position;
    Rotor(vector<int> permutation) {
        position = 0;
        permutation;
    }
    vector<int> GetRotor() const {
        return permutation;
    }
};

class Enigma {
public:
    vector<Rotor> rotors;

    void AddRotor(Rotor rotor) {
        rotors.push_back(rotor);
    }
    void PrintRotor(const vector<Rotor>& rotors) {
     cout << rotors[0].GetRotor();                       // Error right here
     cout << rotors[0].position;
    }
    void setupRotor(int index) {
        Rotor rotor = rotors[index];
    }
    void MoveRotor(int index) {
        rotors[index].position++;
        cout << "Before" << endl;
    //    cout << rotors[index].permutation.data << ' ';
        Enigma::PrintRotor(rotors);
        rotate(rotors[index].permutation.begin(), rotors[index].permutation.begin() + rotors[index].permutation.size(), rotors[index].permutation.end());
        cout << "After" << endl;
        Enigma::PrintRotor(rotors);
    }
};

vector<int> take_numbers(int number) {
    vector<int> numbers;
    for (int i = 0; i < number; i++) {
        int number;
        cin >> number;
        numbers.push_back(number);
    }
    return numbers;
}

void take_rotors(int number_letters, Enigma* enigma) {
    int numberOfRotors;
 //   int numberOfNotch, positionOfNotch;
    cout << "Give number of Rotors" << endl;
    cin >> numberOfRotors;
    for (int i=0; i < numberOfRotors; i++) {
        vector<int> permutation = take_numbers(number_letters);
        Rotor Rotor(permutation);
        enigma->AddRotor(Rotor);                       // I am not sure if it adds Rotors fine.
    }
}

int main()
{
    Enigma enigma;
    int number_letters, move_rotor;
    cout << "Give number of letters in alphabet" << endl;
    cin >> number_letters;
    take_rotors(number_letters, &enigma);
//    take_reflectors(number_letters, &enigma);
    cout << "Which rotor do you want to move (index)" << endl;
    cin >> move_rotor;
    enigma.MoveRotor(move_rotor);

    return 0;
}


标签: c++classvector

解决方案


There is no operator<<(std::ostream&,const std::vector<int>&) if you want it you need to supply your own. However, overloading operators for types you don't own is not recommended, so I would rather write a function:

void print_vector(std::ostream& out, const std::vector<int>& vect) {
    for (int i : vect) {
        out << i << '\n';
    }
}

That you can call like this

print_vector(std::cout, rotors[0].GetRotor());

Alternatively you can supply an overload for << that prints all the Rotor:

std::ostream& operator<<(std::ostream&,const Rotor& rotor) {
    out << rotor.position;
    for (auto& i : rotor.GetRotor()) {
        out << i;
    }
    // modify and add more to your likings
    return out;
}

Once you have that you can also provide an overload to print a vector of rotors that you can use in Enigma::PrintRotor (which currently only prints the first element of the vector):

std::ostream& operator<<(std::ostream& out,const std::vector<Rotor>& rotors) {
    for (const auto& r : rotors) {
        out << r << '\n';
    }
    return out;
}

PS your naming is a little confusing. A Rotor has a GetRotor which returns permutations !?! I strongly suggest to use better names. If I have Rotor r; then r is the Rotor and it is not obvious what a GetRotor will do. Maybe rename it to GetPermutations?


推荐阅读