在 C++ 中将 class 实例添加到对象层次结构时出现问题
Problems adding a class instance to a hierarchy of objects in C++
在我们的项目中,我们需要创建三个 classes:Customers、Campaigns 和 Advertisements。每个客户都有一个活动向量,每个活动都有一个广告向量:
客户 -> 活动 -> 广告
当我们尝试将 Advertisement 实例添加到
给定客户的营销活动。
当我们尝试将广告添加到给定的广告系列时,我们从未看到它
实际上添加了广告。广告数量停滞不前。
我们都是 C++ 的新手,不确定如何使用我们认为与问题相关的引用来解决这个问题。
客户class:
#pragma once
#include <vector>
#include <cstdio>
#include <string>
#include "campaign.h"
using namespace std;
class Customer
{
string name;
int id;
vector<Campaign> campaigns;
public:
Customer(string name, int id)
{
this->name = name;
this->id = id;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
bool AddCampaign(Campaign campaignObject)
{
campaigns.push_back(campaignObject);
return true;
}
bool CommitAdvertisement(Ad ad, int campaignID)
{
for (int i = 0; i < campaigns.size(); i++)
{
if (campaigns[i].GetId() == campaignID)
{
campaigns[i].CommitAdvertisement(ad);
return true;
}
}
return false;
}
bool hasActiveCampaigns()
{
for (Campaign i : campaigns)
{
if (i.IsActive())
{
return true;
}
}
return false;
}
vector<Campaign> GetAllCampaigns()
{
return campaigns;
}
vector<Ad> GetAllAdsForCampaign(int campaignID)
{
for (int i = 0; i < campaigns.size(); i++)
{
if (campaigns[i].GetId() == campaignID)
{
return campaigns[i].GetAllAds();
}
}
}
};
战役class:
#pragma once
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include <ctime>
#include "Ad.h"
using namespace std;
class Campaign
{
string name;
int id;
time_t fromDateTime;
time_t toDateTime;
float campaignCost;
vector<Ad> ads;
public:
Campaign(time_t fromDateTime, time_t toDateTime, int id, string name, float campaignCost)
{
this->fromDateTime = fromDateTime;
this->toDateTime = toDateTime;
this->id = id;
this->name = name;
this->campaignCost = campaignCost;
}
time_t GetFromDateTime()
{
return fromDateTime;
}
void SetFromDateTime(time_t fromDateTime)
{
this->fromDateTime = fromDateTime;
}
time_t GetToDateTime()
{
return toDateTime;
}
void SetToDateTime(time_t toDateTime)
{
this->toDateTime = toDateTime;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
float GetCampaignCost()
{
return campaignCost;
}
void SetCampaignCost(float campaignCost)
{
this->campaignCost = campaignCost;
}
bool IsActive()
{
time_t now = time(NULL);
if (fromDateTime <= now && toDateTime >= now)
{
return true;
}
return false;
}
bool CommitAdvertisement(Ad ad)
{
for (Ad i : ads)
{
if (i.GetId() == ad.GetId())
{
return false;
}
}
ads.push_back(ad);
return true;
}
bool DeleteAdvertisement(int id)
{
for (int i = 0; i < ads.size(); i++)
{
if (ads[i].GetId() == id)
{
ads.erase(ads.begin() + i);
return false;
}
}
return false;
}
vector<Ad> GetAllAds()
{
return ads;
}
};
广告class
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include "AdType.h"
using namespace std;
class Ad
{
private:
string name;
string adText;
AdType adType;
int id;
public:
Ad(string name, string text, int id, AdType type = AdType::PLAINTEXT)
{
this->name = name;
this->adText = text;
this->id = id;
this->adType = type;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
AdType GetType()
{
return adType;
}
void SetType(AdType type)
{
this->adType = type;
}
string GetText()
{
return adText;
}
void SetText(string adText)
{
this->adText = adText;
}
};
现在,我们已经成功地向给定客户添加了一个广告系列,并且能够添加 一个 个广告实例,但不能再添加了。
此代码片段显示了此尝试:
// Create a customer
Customer *the_client = new Customer("Foobar Inc", 123);
// Create a campaign
Campaign *the_campaign = new Campaign(begin, end, 1234, "campaign_name", 123455.0f);
// Create an ad
Ad *the_first_ad = new Ad("First ad", "adtext", 12345656, AdType::PLAINTEXT);
// create another ad
Ad* the_second_ad = new Ad("Second ad", "adtext", 12345656, AdType::PLAINTEXT);
// add the campaign to the customer
the_client->AddCampaign(*the_campaign);
// Add the ad to the customers' list of ads
the_client->CommitAdvertisement(*the_first_ad, 1234);
// Print out how many ads there are in the given campaign
cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;
// Add the second ad to the customers' list of ads
the_client->CommitAdvertisement(*the_second_ad, 1234);
// Again - print out how many ads there are in the given campaign
cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;
很有可能我们完全做错了,但我们非常欢迎任何指导。
CommitAdvertisement
将 return false 并且如果已经有具有相同 id 的广告则添加失败。
这就是你正在做的。第一个和第二个广告的 ID 均为 12345656。所以它正在执行您的要求。
增加 the_second_ad
的 ID。
在我们的项目中,我们需要创建三个 classes:Customers、Campaigns 和 Advertisements。每个客户都有一个活动向量,每个活动都有一个广告向量:
客户 -> 活动 -> 广告
当我们尝试将 Advertisement 实例添加到 给定客户的营销活动。
当我们尝试将广告添加到给定的广告系列时,我们从未看到它 实际上添加了广告。广告数量停滞不前。
我们都是 C++ 的新手,不确定如何使用我们认为与问题相关的引用来解决这个问题。
客户class:
#pragma once
#include <vector>
#include <cstdio>
#include <string>
#include "campaign.h"
using namespace std;
class Customer
{
string name;
int id;
vector<Campaign> campaigns;
public:
Customer(string name, int id)
{
this->name = name;
this->id = id;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
bool AddCampaign(Campaign campaignObject)
{
campaigns.push_back(campaignObject);
return true;
}
bool CommitAdvertisement(Ad ad, int campaignID)
{
for (int i = 0; i < campaigns.size(); i++)
{
if (campaigns[i].GetId() == campaignID)
{
campaigns[i].CommitAdvertisement(ad);
return true;
}
}
return false;
}
bool hasActiveCampaigns()
{
for (Campaign i : campaigns)
{
if (i.IsActive())
{
return true;
}
}
return false;
}
vector<Campaign> GetAllCampaigns()
{
return campaigns;
}
vector<Ad> GetAllAdsForCampaign(int campaignID)
{
for (int i = 0; i < campaigns.size(); i++)
{
if (campaigns[i].GetId() == campaignID)
{
return campaigns[i].GetAllAds();
}
}
}
};
战役class:
#pragma once
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include <ctime>
#include "Ad.h"
using namespace std;
class Campaign
{
string name;
int id;
time_t fromDateTime;
time_t toDateTime;
float campaignCost;
vector<Ad> ads;
public:
Campaign(time_t fromDateTime, time_t toDateTime, int id, string name, float campaignCost)
{
this->fromDateTime = fromDateTime;
this->toDateTime = toDateTime;
this->id = id;
this->name = name;
this->campaignCost = campaignCost;
}
time_t GetFromDateTime()
{
return fromDateTime;
}
void SetFromDateTime(time_t fromDateTime)
{
this->fromDateTime = fromDateTime;
}
time_t GetToDateTime()
{
return toDateTime;
}
void SetToDateTime(time_t toDateTime)
{
this->toDateTime = toDateTime;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
float GetCampaignCost()
{
return campaignCost;
}
void SetCampaignCost(float campaignCost)
{
this->campaignCost = campaignCost;
}
bool IsActive()
{
time_t now = time(NULL);
if (fromDateTime <= now && toDateTime >= now)
{
return true;
}
return false;
}
bool CommitAdvertisement(Ad ad)
{
for (Ad i : ads)
{
if (i.GetId() == ad.GetId())
{
return false;
}
}
ads.push_back(ad);
return true;
}
bool DeleteAdvertisement(int id)
{
for (int i = 0; i < ads.size(); i++)
{
if (ads[i].GetId() == id)
{
ads.erase(ads.begin() + i);
return false;
}
}
return false;
}
vector<Ad> GetAllAds()
{
return ads;
}
};
广告class
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include "AdType.h"
using namespace std;
class Ad
{
private:
string name;
string adText;
AdType adType;
int id;
public:
Ad(string name, string text, int id, AdType type = AdType::PLAINTEXT)
{
this->name = name;
this->adText = text;
this->id = id;
this->adType = type;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
AdType GetType()
{
return adType;
}
void SetType(AdType type)
{
this->adType = type;
}
string GetText()
{
return adText;
}
void SetText(string adText)
{
this->adText = adText;
}
};
现在,我们已经成功地向给定客户添加了一个广告系列,并且能够添加 一个 个广告实例,但不能再添加了。
此代码片段显示了此尝试:
// Create a customer
Customer *the_client = new Customer("Foobar Inc", 123);
// Create a campaign
Campaign *the_campaign = new Campaign(begin, end, 1234, "campaign_name", 123455.0f);
// Create an ad
Ad *the_first_ad = new Ad("First ad", "adtext", 12345656, AdType::PLAINTEXT);
// create another ad
Ad* the_second_ad = new Ad("Second ad", "adtext", 12345656, AdType::PLAINTEXT);
// add the campaign to the customer
the_client->AddCampaign(*the_campaign);
// Add the ad to the customers' list of ads
the_client->CommitAdvertisement(*the_first_ad, 1234);
// Print out how many ads there are in the given campaign
cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;
// Add the second ad to the customers' list of ads
the_client->CommitAdvertisement(*the_second_ad, 1234);
// Again - print out how many ads there are in the given campaign
cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;
很有可能我们完全做错了,但我们非常欢迎任何指导。
CommitAdvertisement
将 return false 并且如果已经有具有相同 id 的广告则添加失败。
这就是你正在做的。第一个和第二个广告的 ID 均为 12345656。所以它正在执行您的要求。
增加 the_second_ad
的 ID。