I have a header file called StudentRecord.cpp which declares the function:

vector<Student>::iterator find (const string& studentName);

In my Student Record.cpp file i have written the folloing code:

vector<Student>::iterator StudentRecord::find(const string& studentName){
   vector<Student>::iterator find;
   for(find = students.begin(); find != students.end(); find++) {
      if (students[find].getName() == studentName) {
         return students[find];
      }
      else {
      cout << "Message: student studentName is not found " << endl;
      }
   }
}

The problem I have in with the part students[find].getName() == studentName. My compiler gives an error no match for operator[] in StudentRecord*this->StudentRecord::students[find]. Please note that Students is a vector object and i wish to find a students name. If the name is found then this function needs to return a vector::iterator type.

If anyone has any idea of how to fix the mistakes? Please Help

Recommended Answers

All 2 Replies

You can't use an iterator with the [] operator, you're supposed to use an integer index with the [] operator.

Why not just use the iterator that you've already defined. e.g. if(find->getName() == studentName){return find;

Thanks dougy83 Absolute Genius!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.