Compiler Error C2676

M

Mike Copeland

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
{
return bibNo < rhs.bibNo;
}
} rrr;

typedef vector<DBSTRUCT> DBVEC;
DBVEC vec;
vector<DBSTRUCT>::iterator dIter;

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

return 0;
}
 
M

maverik

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.
 
A

anon

Mike said:
The following code produces a compiler error (C2676) on the "find"
code line. How can I fix this? TIA

Read what compiler is telling you. You are missing this method:
bool operator==(int)
#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
{
return bibNo < rhs.bibNo;
}

missing this:

bool operator==(const int cmpBibNo) const
{
return bibNo==cmpBibNo;
}
or something like that
} rrr;

typedef vector<DBSTRUCT> DBVEC;
DBVEC vec;
vector<DBSTRUCT>::iterator dIter;

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

return 0;
}
 
M

maverik

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).

25.1.2.p2 (ISO/IEC C++ Standard, 2003):
[Find] Requires: Type T is EqualityComparable (20.1.1).
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

According to the definition (25.1.2 of the same doc)
 
M

maverik

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).

25.1.2.p1 (ISO/IEC C++ Standard, 2003)
[Find]Requires: Type T is EqualityComparable (20.1.1).
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

According to the definition (25.1.2 of the same doc)

If you want to find all elements of the vector "vec" that has Bib
field set to 27, you should try std::find_if() (25.1.2). You can
define your own predicate and pass it to std::find_if().
 
L

Lionel B

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 {
return bibNo < rhs.bibNo;
}

For the find() call below, you need an equality comparison operator
between the object type and the type of the thing you're trying to find
('int' in your case):

bool operator ==(const int n) const
{
return bibNo == n;
}

This should do what (it appears) you want, though I doubt this is terribly
good style.
} rrr;

typedef vector<DBSTRUCT> DBVEC;
DBVEC vec;
vector<DBSTRUCT>::iterator dIter;

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

return 0;
}
 
L

Lionel B

On Nov 25, 5:17 pm, (e-mail address removed) (Mike Copeland) wrote:
[...]
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

I don't believe spaces make any difference here.

[...]
Correct me if I'm wrong.

Sure ;-)
 
L

Lionel B

The following code produces a compiler error (C2676) on the "find" code
line. How can I fix this? TIA
[...]

For the find() call below, you need an equality comparison operator
between the object type and the type of the thing you're trying to find
('int' in your case):

bool operator ==(const int n) const
{
return bibNo == n;
}

This should do what (it appears) you want, though I doubt this is
terribly good style.

Several people have pointed out that find_if() is probably more
appropriate; something like this:

struct bibNo_is
{
const int bibNo;
bibNo_is(const int n) : bibNo(n) {}
bool operator()(const DBSTRUCT& dbs) const {return dbs.bibNo == bibNo;}
};

and then:

[...]

dIter = find_if(vec.begin(), vec.end(), bibNo_is(27));

Cheers,
 
M

maverik

struct bibNo_is
{
    const int bibNo;
    bibNo_is(const int n) : bibNo(n) {}
    bool operator()(const DBSTRUCT& dbs) const {return dbs.bibNo == bibNo;}

};

and then:

[...]

dIter = find_if(vec.begin(), vec.end(), bibNo_is(27));

Why not just:

bool bibno_eq_42(const DBSTRUCT &dbs) { return dbs.bibNo == 42; }

then

dIter = find_if(vec.begin(), vec.end(), bibno_eq_42);
 
T

Thomas J. Gritzan

maverik said:
struct bibNo_is
{
const int bibNo;
bibNo_is(const int n) : bibNo(n) {}
bool operator()(const DBSTRUCT& dbs) const {return dbs.bibNo == bibNo;}

};

and then:

[...]

dIter = find_if(vec.begin(), vec.end(), bibNo_is(27));

Why not just:

bool bibno_eq_42(const DBSTRUCT &dbs) { return dbs.bibNo == 42; }

then

dIter = find_if(vec.begin(), vec.end(), bibno_eq_42);

Because the source file might end up with this:

bool bibno_eq_1(const DBSTRUCT &dbs) { return dbs.bibNo == 1; }
bool bibno_eq_2(const DBSTRUCT &dbs) { return dbs.bibNo == 2; }
bool bibno_eq_3(const DBSTRUCT &dbs) { return dbs.bibNo == 3; }
bool bibno_eq_4(const DBSTRUCT &dbs) { return dbs.bibNo == 4; }
bool bibno_eq_5(const DBSTRUCT &dbs) { return dbs.bibNo == 5; }
....
bool bibno_eq_100(const DBSTRUCT &dbs) { return dbs.bibNo == 100; }
bool bibno_eq_101(const DBSTRUCT &dbs) { return dbs.bibNo == 101; }
....
infinity
 
J

James Kanze

It would help, of course, if you'd tell us what error C2676 is.
I'm not sure if this is a typo or not, but there is must be no
spaces between "operator" and "<". Thus, you get

Nonsense. The keyword operator and the punctuation < are two
separate tokens, and can be separated by any amount of white
space or comments. (As far as the compiler is concerned; as a
reader, I'd rather they not be separated by too much.)
 
L

Lionel B

maverik said:
struct bibNo_is
{
const int bibNo;
bibNo_is(const int n) : bibNo(n) {}
bool operator()(const DBSTRUCT& dbs) const {return dbs.bibNo ==
bibNo;}

};

and then:

[...]

dIter = find_if(vec.begin(), vec.end(), bibNo_is(27));

Why not just:

bool bibno_eq_42(const DBSTRUCT &dbs) { return dbs.bibNo == 42; }

then

dIter = find_if(vec.begin(), vec.end(), bibno_eq_42);

Because the source file might end up with this:

bool bibno_eq_1(const DBSTRUCT &dbs) { return dbs.bibNo == 1; } bool
bibno_eq_2(const DBSTRUCT &dbs) { return dbs.bibNo == 2; } bool
bibno_eq_3(const DBSTRUCT &dbs) { return dbs.bibNo == 3; } bool
bibno_eq_4(const DBSTRUCT &dbs) { return dbs.bibNo == 4; } bool
bibno_eq_5(const DBSTRUCT &dbs) { return dbs.bibNo == 5; } ...
bool bibno_eq_100(const DBSTRUCT &dbs) { return dbs.bibNo == 100; } bool
bibno_eq_101(const DBSTRUCT &dbs) { return dbs.bibNo == 101; } ...
infinity

I guess you could always do:

template<int n>
bool bibNo_eq(const DBSTRUCT& dbs) {return dbs.bibNo == n;}

....

dIter = find_if(vec.begin(), vec.end(), bibNo_eq<27>);

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top