1

So I'm writing this for an assignment for class and I'm learning operator overloading, and I keep getting an error like no operator << matches and binary '>>': no operator found I do have overloading functions for both operators of >> and << in my class, but in my snippet I show the operator overloading for <<

snippet of code:

Source.cpp
int main(void) {
    matrixClassType matrix1(3, 4);
    ofstream fout;
    fout.open("Results.txt");
    fout << "matrix1" << endl;
    static_cast<ostream&>(fout) << matrix1;
}
matrixClassType.h
class matrixClassType
{
    static const int NUMBER_OF_COLUMNS = 10;
    static const int NUMBER_OF_ROWS = 10;
private:
    int matrix[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
    int rowSize;
    int columnSize;
public:
    matrixClassType(void);
    matrixClassType(int row, int column);
    friend ostream& operator << (ofstream& fout, const matrixClassType&);
    ~matrixClassType(void);
matrixClassType.cpp
ostream& operator<<(ofstream& fout, const matrixClassType& matrix)
{
    for (int row = 0; row < matrix.rowSize; row++)
    {
        for (int column = 0; column < matrix.columnSize; column++)
        {
            fout << matrix.matrix[row][column] << " ";
        }
        fout << endl;
    }
    return fout;
}

I also have another friend function overloading for >> that takes in ifstream. However the errors I keep getting are

no operator "<<" matches these operands operand types are: std::basic_ostream<char, std::char_traits<char>> << matrixClassType

Error   C2679   binary '>>': no operator found which takes a right-hand operand of type 'matrixClassType' (or there is no acceptable conversion)
Cassandra
  • 11
  • 1

1 Answers1

0

You can change your main function to the following

int main(void) {
    matrixClassType matrix1(3, 4);
    ofstream fout;
    fout.open("Results.txt");
    fout << "matrix1" << endl;
    fout << matrix1;
}

This is because your definition of the operator << is for ofstream reference, not ostream reference.

CS Pei
  • 10,869
  • 1
  • 27
  • 46