maverik
11/25/2008 2:40:00 PM
On Nov 25, 5:17 pm, mrc2...@cox.net (Mike Copeland) wrote:
> The following code produces a compiler error (C2676) on the "find"
> code line. How can I fix this? TIA
>
> #include <algorithm>
> #include <iostream>
> #include <vector>
>
> using namespace std;
>
> struct DBRECORD
> {
> int Bib;
> int Age;
> string Name;
>
> } ;
>
> struct DBSTRUCT
> {
> int bibNo;
> DBRECORD dWork;
> bool operator <(const DBSTRUCT &rhs) const // comparison operator
I'm not sure if this is a typo or not, but there is must be no spaces
between "operator" and "<". Thus, you get
bool operator<(const DBSTRUCT &rhs) const // comparison operator
> {
> return bibNo < rhs.bibNo;
> }
>
> } rrr;
>
> typedef vector<DBSTRUCT> DBVEC;
> DBVEC vec;
> vector<DBSTRUCT>::iterator dIter;
Why not DBVEC::iterator dIter;
Global variables are evil.
>
> int main(int argc, char *argv[]) // Main Line
> {
>
> rrr.bibNo = rrr.dWork.Bib = 17, rrr.dWork.Age = 39;
> rrr.dWork.Name = "George";
> vec.push_back(rrr);
> rrr.bibNo = rrr.dWork.Bib = 13, rrr.dWork.Age = 29;
> rrr.dWork.Name = "Sam";
> vec.push_back(rrr);
> rrr.bibNo = rrr.dWork.Bib = 27, rrr.dWork.Age = 19;
> rrr.dWork.Name = "David";
> vec.push_back(rrr);
> rrr.bibNo = rrr.dWork.Bib = 47, rrr.dWork.Age = 59;
> rrr.dWork.Name = "Robert";
> vec.push_back(rrr);
> rrr.bibNo = rrr.dWork.Bib = 37, rrr.dWork.Age = 49;
> rrr.dWork.Name = "Ron";
> vec.push_back(rrr);
> sort(vec.begin(), vec.end());
>
> dIter = find(vec.begin(), vec.end(), 27);// <- error C2676
Ok.
1. If you want to find some elements of the container that equals
specified value then you also should define == operator (in case of
user defined types).
2. std::find() the value you want to find. I understand that you want
to find all elements of the vector "vec" that has Bib field set to 27,
but in this case
find(vec.begin(), vec.end(), 27);// <- error C2676
you shoul pass the variable of the same type as vector elements type:
DBSTRUCT
Correct me if I'm wrong.