Перегрузка ostream выдает ошибку «Недопустимые операнды в бинарном выражении» - PullRequest
0 голосов
/ 29 октября 2018

У меня есть задание, в котором я должен перегрузить операторы для класса «Собака». Основная функция, приведенная в моем назначении, имеет операторы cout, которые по какой-то причине добавляют / вычитают / умножают / делят, увеличивают объекты вместе, хотя те, которые делают это, приводят к ошибке «Недопустимые операнды для двоичного выражения ('std :: __ 1 :: ostream») '(aka' basic_ostream ') и' Dog ') " Если кто-то видит проблему, пожалуйста, дайте мне знать, я устраняю неполадки уже более часа. Заранее спасибо.

// TODO: Implement the addition operator overload here.
Dog Dog::operator+(const Dog& doggo2) const {
    Dog doggo3;
    doggo3.weight = weight + doggo2.weight;
    doggo3.height = height + doggo2.height;
    return doggo3;
}

// TODO: Implement the subtraction operator overload here.
// Remember that boxes cannot have negative dimensions.
Dog Dog::operator-(const Dog& doggo2) const {
    Dog doggo3;
    doggo3.weight = weight - doggo2.weight;
    doggo3.height = height - doggo2.height;
    if (doggo3.weight < 0) {
        doggo3.weight = 0;
    }
    if (doggo3.height < 0) {
        doggo3.height = 0;
    }
    return doggo3;
}

// TODO: Implement the multiplication operator overload here.
Dog Dog::operator*(const Dog& doggo2) const {
    Dog doggo3;
    doggo3.weight = weight * doggo2.weight;
    doggo3.height = height * doggo2.height;
    return doggo3;
}

// TODO: Implement the division operator overload here.
// Remember that division by zero is undefined.       
Dog Dog::operator/(const Dog& doggo2) const {
    Dog doggo3;
    if (doggo2.weight != 0) {
        doggo3.weight = weight / doggo2.weight;
    }
    else {
        doggo3.weight = 0;
    }
    if (doggo2.height != 0) {
        doggo3.height = height / doggo2.height;
    }
    else {
        doggo3.height = 0;
    }
    return doggo3;
}

// TODO: Implement the greater than operator overload here.
bool Dog::operator>(const Dog& doggo2) const {
    return ((height*weight) > (doggo2.height*doggo2.weight));
}

// TODO: Implement the less than operator overload here.
bool Dog::operator<(const Dog& doggo2) const {
    return ((height*weight) < (doggo2.height*doggo2.weight));
}

// TODO: Implement the equality operator overload here.
bool Dog::operator==(const Dog& doggo2) const {
    return ((height*weight) == (doggo2.height*doggo2.weight));
}

// TODO: Implement the not-equal-to operator overload here.
bool Dog::operator!=(const Dog& doggo2) const {
    return ((height*weight) != (doggo2.height*doggo2.weight));
}

// TODO: Implement the post-increment operator overload here.
Dog Dog::operator++(int u){
    weight++;
    height++;
    return *this;
}

// TODO: Implement the pre-increment operator overload here.
Dog Dog::operator++(){
    ++weight;
    ++height;
    return *this;
}

// TODO: Implement the post-decrement operator overload here.
Dog Dog::operator--(int u){
    weight--;
    height--;
    return *this;
}

// TODO: Implement the pre-decrement operator overload here.
Dog Dog::operator--(){
    --weight;
    --height;
    return *this;
}

// TODO: Implement the stream insertion operator overload here.
istream& operator>>(istream& iObject, Dog& doggo) {
    iObject >> doggo.weight >> doggo.height;
    return iObject;
}

// TODO: Implement the stream extraction operator overload here.
ostream& operator<<(ostream& oObject, Dog& doggo) {
    oObject << "Look at my pupper uwu!!! He weighs " << doggo.weight << " and is " << doggo.height << " inches tall!!! He's such a good boy :3" << endl;
    return oObject;
}

И основная функция ниже

#include "Dog.h"
#include <iostream>
using namespace std;

int main()
{
    Dog dog1;
    Dog dog2;

    cout << "Space separated, enter the weight (lb) and height (in) of dog1: ";
    cin >> dog1;
    cout << "cout << dog1;\n";
    cout << dog1;

    cout << "Space separated, enter the weight (lb) and height (in) of dog2: ";
    cin >> dog2;
    cout << "cout << dog2;\n";
    cout << dog2;

    cout << "dog1 + dog2;\n";
    cout << dog1 + dog2;
    cout << "cout << dog1 - dog2;\n";
    cout << dog1 - dog2;
    cout << "cout << dog1 * dog2;\n";
    cout << dog1 * dog2;
    cout << "cout << dog1 / dog2;\n";
    cout << dog1 / dog2;

    if (dog1 > dog2)
        cout << "dog1 > dog2" << endl;
    if (dog1 < dog2)
        cout << "dog1 < dog2" << endl;
    if (dog1 == dog2)
        cout << "dog1 == dog2" << endl;
    if (dog1 != dog2)
        cout << "dog1 != dog2" << endl;

    cout << "cout << ++dog1;\n";
    cout << ++dog1;
    cout << "cout << --dog2;\n";
    cout << --dog2;

    Dog dog3 = dog2++;
    cout << "Dog dog3 = dog2++;" << endl;
    cout << "cout << dog3;\n";
    cout << dog3;
    cout << "cout << dog2;\n";
    cout << dog2;

    system("pause");
    return 0;
}

1 Ответ

0 голосов
/ 29 октября 2018

Так как вы не сообщили о том, как реализовали класс Dog, у меня есть его простая реализация.

using namespace std;

class Dog
{
   double m_weight, m_height;
public:

    Dog(double weight, double height):m_weight(weight), m_height(height){}

    friend ostream& operator<<(ostream& os, const Dog& dog); // const Dog
};

ostream& operator<<(ostream& os, const Dog& dog)
{
    os<<"Weight="<<dog.m_weight<<"  Height="<<dog.m_height;

    return os;
}

int main()
{
  Dog dog(2.0,3.0);
  cout<<dog;

  return 0;
}
...