Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

c++ - Find and delete value in a vector

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) ()’

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your error is here:

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

Change that line to this:

if(Transit->StationTitle == ToDelete)

OR

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...