class Catalog
{
// string StationTitle;
string StationLocation;
public:
string StationTitle;
Catalog()
{
StationTitle = "";
StationLocation = "";
}
Catalog(string Title, string Location)
{
StationTitle = Title;
StationLocation = Location
}
void SetTitle(string Title) { StationTitle = Title; }
void SetLocation(string Location) { StationLocation = Location; }
string GetTitle() { return StationTitle; }
string GetLocation() { return StationLocation; }
};
class StationList
{
vector<Catalog> List; //create the vector
vector<Catalog>::iterator Transit;
public:
void Fill();
void Remove();
void Show();
};
void StationList::Remove()
{
string ToDelete;
cout << "Enter title to delete: " << endl;
cin >> ToDelete;
for(Transit = List.begin() ; Transit !=List.end() ; Transit++)
{
if(Transit->StationTitle() == ToDelete)
{
List.erase(Transit); //line 145
return;
}
}
}
我希望用戶輸入StationTitle並讓程序找到標題並將其刪除(如果找到)。這是我迄今爲止所提出的。
它給了我一個編譯錯誤:chief.cpp:145:錯誤:不匹配呼叫'(std :: string)()'查找和刪除矢量中的值
您可以添加聲明交通? – JoshD 2010-10-05 01:23:10
這是最好的使用擦除刪除成語這種事情。這可以將您的八行縮減爲一行,並且更容易掃描代碼並找出它的功能。 – 2010-10-05 01:24:43
@詹姆斯麥克奈利斯:我可能會誤解你,但是因爲匹配只是元素的一部分,所以如果不重載平等或提供不同的比較函數,remove函數將不起作用。當然,這是一個吸引人的選擇。 – JoshD 2010-10-05 01:28:25