我对所有者文件中的 << 运算符有疑问

I am having problem with the << operator in the owner file

我制作了一个程序,它有两种类型的 classes 实现了 owner 和 dogs ,我在 Owner.h 的 << 运算符中的迭代器部分有问题文件,我知道我们不能对类型为自定义 class 的 STL 容器使用迭代器,但是这样做的替代方法是什么?

我附上了供您参考的主文件

我知道你们有很多问题要问,但请向我推荐一种方法,我会解决的 谢谢

主文件

#include <iostream>

#include "Owner.h"

#include "Dog.h"



using namespace std;



void Purchase(Owner& owner, Dog& dog) {

    owner.AddDog(dog);

    dog.SetOwner(owner);

}



int main() {

    Owner owner1("Michael Hagley", "14 Brentwood Terrace");

    Owner owner2("Oliver Walter", "299 Mill Road");

    Dog dog1("Cheeto", 5, 2000.00);

    Dog dog2("Mavrick", 8, 1800.00);

    Dog dog3("Biglet", 4, 2100.00);

    Dog dog4("Snoopy", 11, 600.00);

    Dog dog5("Leggo", 2, 500.00);

    Dog dog6("Bugsy", 4, 1100.00);



    Purchase(owner1, dog1);

    Purchase(owner1, dog5);

    Purchase(owner2, dog2);

    Purchase(owner2, dog3);

    Purchase(owner2, dog6);

    cout<<owner1.GetName()<<" Has the following dogs"<<std::endl;
    cout<<std::endl;
    cout<<owner2.GetName()<<"has the following dogs"<<std::endl;




//    cout << owner1;
//
//    cout << owner2;



    Owner* owner = dog1.GetOwner();

    if (owner) cout << dog1.GetName() << " belongs to " << owner->GetName() << endl;



    owner = dog4.GetOwner();

    if (owner){
        cout << dog4.GetName() << " belongs to " << owner->GetName() << endl;
    }
    else{
        cout<<dog4.GetName()<<" Has no owner"<<endl;
    }



    return 0;

}

所有者文件

//
// Created by Kannav Sethi on 21/04/22.
//

#ifndef FINALS_OWNER_H
#define FINALS_OWNER_H
#include <iostream>
#include <vector>
//Wwe'll be using the concept of association and aggregation here
class Dog;
class Owner{
//    provided the variables for the Owner here
// the only viable STL container here is vectors of type dogs
//but there is a problem with it , I wont be able to iterate over the dogs vector due to it being of a custom type
//Lets try that out here

    std::string name;
    std::string address;
    std::vector<Dog> dogs;
    int size=0;

public:
//    constructor
    Owner(){
        name="";
        address="";
    };
//constructor
    Owner(std::string name,std::string address){
        this->name=name;
        this->address=address;
    };
//    tried out making a copy assignment but it was futile
//    Owner& operator=(Owner& oTemp){
//        this->name=oTemp.name;
//        this->address=oTemp.address;
//        for(auto i = oTemp.dogs.begin();i!=oTemp.dogs.end();i++){
//            this->dogs.push_back(i);
//        }
//        return *this;
//    }

//add dog
    void AddDog(Dog& dog){
        dogs.push_back(dog);
        size++;
    }
//get name function
    std::string GetName(){
        return this->name;
    }
    friend std::ostream& operator<<(std::ostream& os,Owner& owner);
};
std::ostream& operator<<(std::ostream& os,Owner& owner){
   std::vector<Dog>::iterator it;
   os<<owner.name<<"of "<<owner.address<<" has the following dogs"<<std::endl;
   double sum=0;
   for(auto it=owner.dogs.begin();it!=owner.dogs.end();it){
      it.operator++();
      sum+=it.cost;


  }
   std::cout<<"the total costs of all the dogs are $"<<sum<<std::endl;


#endif //FINALS_OWNER_H

狗档

//
// Created by Kannav Sethi on 21/04/22.
//

#ifndef FINALS_DOG_H
#define FINALS_DOG_H
#include <iostream>
#include "Owner.h"

class Owner;
class Dog{
//    the variables
    std::string name;
    int age;
    double cost;
    Owner* owner;

public:
//  constructors
    Dog(std::string name,int age,double cost){
        this->name=name;
        this->age=age;
        this->cost=cost;

    }
//    set owner
    void SetOwner(Owner& _owner){
        this->owner=(&_owner);
    }
//    get owner
       Owner* GetOwner(){
        return this->owner;

    }
//    getName
    std::string GetName(){
        return this->name;
    }
//    this streaming operator works but not the one I used in Owner
   friend std::ostream& operator<<(std::ostream& os, Dog dog);

};
 std::ostream& operator<<(std::ostream& os, Dog dog){
     os<<dog.name<<" is "<<dog.age<<" years old and costs "<<dog.cost<<std::endl;

}

#endif //FINALS_DOG_H

你需要

std::ostream& operator<<(std::ostream& os, Owner& owner) {
    os << owner.name << "of " << owner.address << " has the following dogs" << std::endl;
    double sum = 0;
    for (auto it = owner.dogs.begin(); it != owner.dogs.end(); it++) {
 
        sum += it->cost;
        std::cout << it->name << std::endl;

    }
    std::cout << "the total costs of all the dogs are $" << sum << std::endl;
    return os;
}

您似乎没有意识到必须将“->”与迭代器一起使用

您还需要将其设为狗的朋友(费用不公开)

//    this streaming operator works but not the one I used in Owner
friend std::ostream& operator<<(std::ostream& os, Dog dog);
friend std::ostream& operator<<(std::ostream& os, Owner& o);