我制作了一個程式,它有兩種實作 owner 和 dogs 的類,我在 Owner.h 檔案中的 << 運算子中的迭代器部分有問題,我知道我們不能使用迭代器對于將其型別作為自定義類的 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
uj5u.com熱心網友回復:
你需要
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;
}
您似乎沒有意識到您必須將“->”與迭代器一起使用
您還需要將此設為 Dog 的朋友(費用為私人)
// 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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/464681.html
上一篇:作為成員變數的意外迭代器行為
