在 C++ 中将类实例添加到对象层次结构时出现问题
Posted
技术标签:
【中文标题】在 C++ 中将类实例添加到对象层次结构时出现问题【英文标题】:Problems adding a class instance to a hierarchy of objects in C++ 【发布时间】:2019-11-13 12:51:28 【问题描述】:在我们的项目中,我们需要创建三个类:客户、活动和广告。每个客户都有一个营销活动向量,每个营销活动都有一个广告向量:
客户 -> 活动 -> 广告
当我们尝试将广告实例添加到 给定客户的广告系列。
当我们尝试将广告添加到给定的广告系列时,我们从未看到它 实际添加广告。广告数量保持不变。
我们都是 C++ 新手,不知道如何通过引用来解决这个问题,我们认为这与问题有关。
客户类:
#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();
;
广告系列类:
#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;
;
广告类
#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;
;
现在,我们已经成功地向给定客户添加了一个广告系列,并且能够添加 一个 广告实例,但不能再添加了。
这段代码 sn-p 显示了这种尝试:
// 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;
很可能我们完全做错了,但我们非常欢迎任何指导。
【问题讨论】:
终生提示:检查返回值 ;-)。顺便说一句,由于尝试两次添加相同的广告可能是(意料之外的,在“不应该发生”的意义上)错误,因此可以考虑抛出异常。这使代码从错误检查和处理中变得杂乱无章(默认处理程序是 abort())。 是的,当然。非常感谢,我去看看! 【参考方案1】:CommitAdvertisement
如果已经有相同id的广告,会返回false,添加失败。
这就是你正在做的事情。第一个和第二个广告的 id 都是 12345656。所以它按照你的要求做。
增加the_second_ad
的ID。
【讨论】:
哇,我觉得自己很愚蠢。这就是用新语言不停编码的日子所做的。愚蠢的复制粘贴。谢谢你的回答!以上是关于在 C++ 中将类实例添加到对象层次结构时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
将 C++ 结构编组到 C# 类时出现 AccessViolationException
在 lejos 中将 Lines 添加到 LineMap 时出现空指针异常,找不到
将结构前向声明为类时出现 Visual C++ 2015 链接器错误