Find and delete value in a vector

吃可爱长大的小学妹 提交于 2019-12-14 03:32:17

问题


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;
    }
  }
}

I would like the user to enter in a StationTitle and for the program to locate the title and delete it if found. This is what I have come up with so far.
It is giving me a compile error: chief.cpp:145: error: no match for call to ‘(std::string) ()’


回答1:


Your error is here:

 if(Transit->StationTitle() == ToDelete)

Change that line to this:

if(Transit->StationTitle == ToDelete)

OR

if(Transit->GetTitle() == ToDelete)



回答2:


It seems like StationTitle is a property of the class in Transit, not a function. Can't be sure without more code.



来源:https://stackoverflow.com/questions/3860271/find-and-delete-value-in-a-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!