[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Compiler Error C2676

mrc2323

11/25/2008 2:17:00 PM

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

maverik

11/25/2008 2:40:00 PM

0

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.

anon

11/25/2008 2:45:00 PM

0

Mike Copeland wrote:
> 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;
> }

maverik

11/25/2008 2:52:00 PM

0

On Nov 25, 5:39 pm, maverik <maverik.m...@gmail.com> wrote:
> 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)

maverik

11/25/2008 2:57:00 PM

0

On Nov 25, 5:39 pm, maverik <maverik.m...@gmail.com> wrote:
> 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().

jt

11/25/2008 2:58:00 PM

0

On Tue, 25 Nov 2008 07:17:27 -0700, 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 {
> 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;
> }





--
Lionel B

jt

11/25/2008 3:00:00 PM

0

On Tue, 25 Nov 2008 06:39:32 -0800, maverik wrote:

> On Nov 25, 5:17 pm, mrc2...@cox.net (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 ;-)

--
Lionel B

ebony.soft

11/25/2008 3:03:00 PM

0

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
>     {
>         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;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

Hi Mike

We got C2784 compiler error which indicates could not deduce template
argument for 'type' from 'type'. vec is a vector of DBSTRUCT and you
like to find a person
with an specific Bib number. So the compiler can't deduce the correct
type.
std::find finds an specific object using operator==. You have to use
find_if and write
a proper function object.

Good luck
Saeed Amrollahi

jt

11/25/2008 3:13:00 PM

0

On Tue, 25 Nov 2008 14:58:13 +0000, Lionel B wrote:

> On Tue, 25 Nov 2008 07:17:27 -0700, Mike Copeland wrote:
>
>> 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,

--
Lionel B

red floyd

11/25/2008 5:12:00 PM

0

On Nov 25, 6:17 am, 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
> [code redacted]

You know, there are other C++ compilers in the world besides
Microsoft's. ISO/IEC 14882:2003 does not specifiy what error code
C2676 is. If you want help, it might be more useful to post the
*TEXT* of the error message.

See http://www.parashift.com/c++-faq-lite/how-to-post.ht...

maverik

11/25/2008 5:59:00 PM

0

On Nov 25, 6:12 pm, Lionel B <m...@privacy.net> wrote:
> 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);