Did handling of "operator<<" in g++ change between v4.8.5 and v8.4.1?

70 Views Asked by At

I have a large code that compiled fine in g++ 4.8.5 (on RHEL7) but fails in 8.4.1 (on RHEL8) (I do not have any intermediate g++ versions currently available). The compiling fails in a certain file that uses operator overloading for "<<". Specifically, the v8.4.1 compiler has issues with a line of code like this:

out << robj.print( out );

v4.8.5 does not issue any warning/error at all. It simply creates an o file. However, v8.4.1 issues the following error:

no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::ostream’ {aka ‘std::basic_ostream<char>’})
cout << out;

My code is too long to post, but I found a similar issue here (C++ class with ostream operator works with VS and fails to compile with gcc) which I modified to show my particular issue. I also included the suggestions/updates from "osgx" from that post. I am posting the modified files:

rational.h

#ifndef  RATIONAL_H
#define RATIONAL_H
#include <iostream>
#include <ostream>
#include <cmath>
#include <cstdlib>
using namespace std;

class rational
{
public:
    rational(int n = 0, int d = 1);
    rational multiply(const rational &r2) const;

    friend ostream& operator<<(ostream &out, const rational &);

    ostream& print(ostream& out) const {
        cout << out;    
    };

private:
    int num;    // numerator
    int denom;  // denominator
};

#endif

rational.cpp (the compiling error occurs in this file)

#include "rational.h"
using namespace std;

rational::rational(int n, int d)
{
    num = n;
    denom = d;
}

rational rational::multiply(const rational &r2) const
{
    rational multi;
    multi.denom = (*this).denom * r2.denom;
    multi.num = (*this).num * r2.num;
    return multi;
}

ostream& operator<<(ostream &out, const rational &robj)
{
    out << robj.num << "/" << robj.denom;   
    // The v8.4.1 compiler complains about this next line
    out << robj.print( out );
    
    return out;
}

main.cpp

#include "rational.h"

using namespace std;

int main()
{
    rational r1(1,4), r2(1,3),r3;

    cout << "r1 initialized to: " << r1 << endl;
    cout << "r2 initialized to: " << r2 << endl << endl;

    cout << "Testing multiplication function:" << endl;
    cout << "\tr1 * r2 = " << r1 << " * " << r2 << " = " << r1.multiply(r2) << endl;
}

Compiling command: g++ -c main.cpp rational.cpp

Hoping someone can spot the issue. Thanks!

0

There are 0 best solutions below