У меня есть эти файлы:
Fraction.h
#ifndef UNTITLED_FRACTION_H
#define UNTITLED_FRACTION_H
#import "operators.h"
class Fraction {
private:
int m_numerator;
int m_denominator;
public:
Fraction(int numerator, int denominator):m_numerator(numerator),m_denominator(denominator)
{
}
Fraction():m_numerator(0),m_denominator(1)
{
}
void print();
friend Fraction operator*(const Fraction &m, int value);
friend Fraction operator*(const Fraction &m, const Fraction &m2);
friend Fraction operator*(int value, const Fraction &m);
friend std::ostream& operator<< (std::ostream &out, const Fraction &frac);
};
#endif //UNTITLED_FRACTION_H
operator.h
#ifndef UNTITLED_OPERATORS_H
#define UNTITLED_OPERATORS_H
#include <iosfwd>
#include "Fraction.h"
#include <string>
class Fraction;
Fraction operator*(const Fraction &m, int value);
Fraction operator*(const Fraction &m, const Fraction &m2);
Fraction operator*(int value, const Fraction &m);
std::ostream& operator<< (std::ostream &out, const Fraction &frac);
int gcd(int a, int b);
#endif //UNTITLED_OPERATORS_H
операторов. cpp
#include "operators.h"
std::ostream& operator<< (std::ostream &out, const Fraction &frac)
{
out << frac.m_numerator << "/" << std::to_string(frac.m_denominator);
return out;
}
Я получаю сообщение об ошибке в операторах. cpp, говоря:
Invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'const int')
Но в https://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/ они делают то же самое и работают. Похоже, я не могу объединить std :: ostream с целым числом, но это обычно работает, когда мы объединяем ostreams и целые числа в std :: cout - т.е. std :: cout << "Hello" << 4; </p>