Home > Back-end >  I am having problem with the << operator in the owner file
I am having problem with the << operator in the owner file

Time:04-22

I have made a programme that has two types of classes that implements owner as well as dogs , I am having a problem with the iterator part in the << operator in the Owner.h file, I know that we can't use an iterator for an STL container having its type as a custom class , but then what is the alternative to do so ?

I am attaching the files for you reference with the main file

I know this is a lot to ask from you guys , but just recommend me a way and i'll figure it out Thanks

Main File

#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;

}

Owner File

//
// 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

Dog file

//
// 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

CodePudding user response:

You need

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;
}

You didnt seem to realize that you have to use '->' with an iterator

You also need to make this a friend of Dog (cost is private)

//    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);
  • Related