0

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!

fabian
  • 80,457
  • 12
  • 86
  • 114
bjs_3214
  • 11
  • 1
  • 2
    `ostream& print(ostream& out) const` doesn't return. – Jarod42 Jan 06 '23 at 15:53
  • 1
    Can you explain what you trying to achieve by `out << robj.print( out );` instead of just `robj.print( out );`? Especially considering that you doesn't return anything from `print()`. – sklott Jan 06 '23 at 15:53
  • 1
    In `ostream& print(ostream& out) const` - `cout << out;` this just won't compile for me – Richard Critten Jan 06 '23 at 15:54
  • 1
    I would say that old g++ 4.8.5 wrongly accept the code. – Jarod42 Jan 06 '23 at 15:57
  • Sidenote. `using namespace std;` is bad thing. But doing so in header files is even worse, Don't do this! https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?r=SearchResults&s=1%7C1565.6306 – sklott Jan 06 '23 at 15:57
  • 3
    One way or the other the `print` function doesn't return a value, so even if your program compiles, its behaviour is undefined... – fabian Jan 06 '23 at 15:59
  • It looks like you just need to delete function `print()` and all its invokations and everything should work as expected. At least for this code. Not sure about other code that you can't show... – sklott Jan 06 '23 at 16:00
  • Coding tip: `(*this).denom` can be written `denom`. Some folks like to decorate mentions of member names with `this->`; mentioning `this` for direct member access (whether with `*this` or `this->`) is redundant, and distracting. – Pete Becker Jan 06 '23 at 16:00
  • Undefined behaviour works that way. Using the value from a function that does not return a value is undefined. – Stephen M. Webb Jan 06 '23 at 16:08

0 Answers0