gpt4 book ai didi

c++ - STL 设置未正确添加 c++

转载 作者:行者123 更新时间:2023-11-30 02:12:06 24 4
gpt4 key购买 nike

我正在将 musicCD 信息添加到集合中。为此,我有两个不同的功能。问题是添加音乐家。它只是添加最后一位传入的音乐家,就像它复制第一个音乐家一样。

这是让您了解信息所需的输出。只有 ringo starr 添加,而不是“George Harrison”“John Lennon”。

    -MusicCD-
band: Beatles
musicians: "George Harrison" "John Lennon" "Ringo Starr"
songs: 10
title: Sergeant Pepper's Lonely Hearts Club Band
keywords: acid rock, sixties

这里是main()

item = library->addMusicCD("Sergeant Pepper's Lonely Hearts Club Band", "Beatles", 10);
if (item != NULL) {
library->addKeywordForItem(item, "acid rock");
library->addKeywordForItem(item, "sixties");
library->addBandMember(item, "John Lennon");
library->addBandMember(item, "George Harrison");
library->addBandMember(item, "Ringo Starr");
library->printItem(cout, item);
}

这是我要添加信息的 libray.h 文件

#include "Library.h"
#include "book.h"
#include "cd.h"
#include "dvd.h"

#include <iostream>
// general functions


ItemSet allBooks;
ItemSet allCDS;
ItemSet allDVDs;
ItemSet* temp;



ItemSetMap allBooksByAuthor;
ItemSetMap allmoviesByDirector;
ItemSetMap allmoviesByActor;

ItemSetMap allMusicByBand;
ItemSetMap allMusicByMusician;

const Item* Library::addMusicCD(const string& title, const string& band, const int nSongs)
{

ItemSet* obj = new ItemSet();
CD* item = new CD(title,band,nSongs);

allCDS.insert(item);
obj->insert(item);
//allMusicByBand[band] = obj;

ItemSetMap::iterator myband = allMusicByBand.find(band);

if(myband != allMusicByBand.end())
{
myband->second->insert(item);

}
else{
allMusicByBand.insert(make_pair(band, obj));
}

return item;

}

void Library::addBandMember(const Item* musicCD, const string& member)
{

ItemSet* obj = new ItemSet();

(((CD*) musicCD)->addBandMember(member));
obj->insert((CD*) musicCD);


ItemSetMap::iterator MByMusician = allMusicByMusician.find(member);

if(MByMusician != allMusicByMusician.end())
{
MByMusician->second->insert((CD*) musicCD);


}
else
{
allMusicByMusician.insert(make_pair(member, obj));
}

}

这是 cd.cpp 类

 #include "CD.h"

using namespace std;

CD::CD(const string& theTitle, const string& theBand, const int snumber)
: Item(theTitle), band(theBand),number(snumber)
{



}

CD::~CD()
{
}


const string CD::getBand() const
{
return band;

}


const string CD::getMusician() const
{
return musicians;

}

const int CD::getNumber() const
{

return number;

}

void CD::addBandMember(const string &member)
{


this->musicians = member;

}


void CD::print(ostream &out) const
{

out << "-MusicCD-" << endl;
out << "band: " << this->getBand() << endl;
out << "musicians: " << this->getMusician() << endl;
out << "songs: " << this->getNumber() << endl;
out << "title: " << this->getTitle() << endl;
out << "keywords: " << this->printKeywords(this->getKeywords()) << endl;
out << endl;


}

ostream& operator<<(ostream& out, const CD* cd)
{

cd->print(out);
return out;

}

最后是cd.h

#ifndef CD_H
#define CD_H
#pragma once
#include "item.h"

class CD : public Item
{
public:

CD(const string& theTitle, const string& theBand, const int snumber);
void addBandMember(const string& member);
const int getNumber() const;
const string getMusician() const;

const string getBand() const;
virtual void print(ostream& out) const;
~CD();


private:

string band;
string musicians;
string title;
int number;

};

ostream& operator<<(ostream& out, const CD* cd);

#endif

我必须假设我的问题出在 Library::addBandMember 函数上?

请记住,我无法在库类中更改该函数。

最佳答案

是的。

string musicians;

此变量存储单个字符串。因此,即使 addBandMember 的调用成功,它也会被下一次调用覆盖。因此,您只剩下最后添加的音乐家的名字——在您的例子中是 Ringo。使用 listvector 来容纳所有音乐家。

vector<string> musicians;

并将addBandMember修改为:

void CD::addBandMember(const string &member)
{
this->musicians.push_back(member);
}

关于c++ - STL 设置未正确添加 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2394061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com