我有一个作业,必须重载“狗”类的运算符。 我的作业中给出的主要函数具有cout语句,这些语句可将对象相加/相减/相乘/相除,使对象一起增加,尽管出于某些原因,这样做会给错误“二进制表达式无效的操作数('std :: __ 1 :: ostream '(又称“ 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;
}
答案 0 :(得分:0)
由于您尚未发布实现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;
}