A C2664 error



 DEVELOP > c-Plus-Plus > A C2664 error

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 21 Dec 2007 01:57:21 AM
Object: A C2664 error
Here is a program, I want to change the way which a map use to order
elements.
struct sizemore
{
bool operator()(const wstring &s1,const wstring &s2)
{
return s1.size()>s2.size();
}
};
int dictinput(const char *filename,map<wstring,wstring> &mwdict())
{
...
}
int main()
{
map<wstring,wstring> mwdict(sizemore());
dictinput("filename",mwdict);
}
But I receive a message:
error C2664: 'dictinput' : cannot convert parameter 2 from
'std::map<_Kty,_Ty> (__cdecl *)(sizemore (__cdecl *)(void))' to
'std::map<_Kty,_Ty> &(__cdecl *)(void)'
I'm always puzzled with parameters of function.
Could some one give me some advices?
Thanks a lot.
.

User: "Ondra Holub"

Title: Re: A C2664 error 21 Dec 2007 02:23:54 AM
napsal:

Here is a program, I want to change the way which a map use to order
elements.

struct sizemore
{
bool operator()(const wstring &s1,const wstring &s2)
{
return s1.size()>s2.size();
}
};

int dictinput(const char *filename,map<wstring,wstring> &mwdict())
{
...
}

int main()
{
map<wstring,wstring> mwdict(sizemore());
dictinput("filename",mwdict);
}

But I receive a message:
error C2664: 'dictinput' : cannot convert parameter 2 from
'std::map<_Kty,_Ty> (__cdecl *)(sizemore (__cdecl *)(void))' to
'std::map<_Kty,_Ty> &(__cdecl *)(void)'

I'm always puzzled with parameters of function.
Could some one give me some advices?
Thanks a lot.

You need to specify comparation as 3rd parameter of template std::map.
See example below:
#include <iostream>
#include <map>
#include <string>
template<typename T>
struct CmpBySize
{
bool operator()(const T& s1, const T& s2) const
{
return s1.size() < s2.size();
}
};
typedef
std::map<std::string, std::string, CmpBySize<std::string> >
MapStrStr;
int main()
{
MapStrStr m;
m["aaa"] = "3 x a";
m["bb"] = "2 x b";
m["z"] = "1 x z";
const MapStrStr::const_iterator end = m.end();
for (MapStrStr::const_iterator i = m.begin(); i != end; ++i)
{
std::cout << "m[\"" << i->first << "\"] = \"" << i->second <<
"\"\n";
}
}
.
User: ""

Title: Re: A C2664 error 21 Dec 2007 04:20:56 AM
On 12=D4=C221=C8=D5, =CF=C2=CE=E74=CA=B123=B7=D6, Ondra Holub <ondra.ho...@p=
ost.cz> wrote:

zhx...@gmail.com napsal:





Here is a program, I want to change the way which a map use to order
elements.


struct sizemore
{
bool operator()(const wstring &s1,const wstring &s2)
{
return s1.size()>s2.size();
}
};


int dictinput(const char *filename,map<wstring,wstring> &mwdict())
{
...
}


int main()
{
map<wstring,wstring> mwdict(sizemore());
dictinput("filename",mwdict);
}


But I receive a message:
error C2664: 'dictinput' : cannot convert parameter 2 from
'std::map<_Kty,_Ty> (__cdecl *)(sizemore (__cdecl *)(void))' to
'std::map<_Kty,_Ty> &(__cdecl *)(void)'


I'm always puzzled with parameters of function.
Could some one give me some advices?
Thanks a lot.


You need to specify comparation as 3rd parameter of template std::map.
See example below:

#include <iostream>

#include <map>
#include <string>

template<typename T>
struct CmpBySize
{
bool operator()(const T& s1, const T& s2) const
{
return s1.size() < s2.size();
}

};

typedef
std::map<std::string, std::string, CmpBySize<std::string> >
MapStrStr;

int main()
{
MapStrStr m;
m["aaa"] =3D "3 x a";
m["bb"] =3D "2 x b";
m["z"] =3D "1 x z";

const MapStrStr::const_iterator end =3D m.end();
for (MapStrStr::const_iterator i =3D m.begin(); i !=3D end; ++i)
{
std::cout << "m[\"" << i->first << "\"] =3D \"" << i->second <<
"\"\n";
}



}

I have try your advice,but now the erroer information is:
cannot convert parameter 2 from 'std::map<_Kty,_Ty,_Pr>' to
'std::map<_Kty,_Ty,_Pr> &(__cdecl *)(void)
Is here anything wrong about the parameter of the fuctiocn?
int dictinput(const char
*filename,map<wstring,wstring,sizemore<wstring>> &mwdict());
And I sent a map<wstring,wstring,sizemore<wstring> object for the 3rd
parameter.
.
User: ""

Title: Re: A C2664 error 21 Dec 2007 04:56:17 AM
Oh I think I make a mistake, it's should not be &mwdic() but mwdic.
Thank you very much :)
.



User: "Tadeusz B. Kopec"

Title: Re: A C2664 error 21 Dec 2007 02:52:00 PM
On Thu, 20 Dec 2007 23:57:21 -0800, zhxzhx wrote:

Here is a program, I want to change the way which a map use to order
elements.

struct sizemore
{
bool operator()(const wstring &s1,const wstring &s2) {
return s1.size()>s2.size();
}
};

[snip]
Luckily you have already found the problem so I only want to ask you - do
you realise your comparison function treats all strings with equal size
as equal? So you may have in your map as a key either "cat" or "dog", but
not both.
--
Tadeusz B. Kopec (tkopec@NOSPAMPLEASElife.pl)
Any fool can tell the truth, but it requires a man of sense to know
how to lie well.
-- Samuel Butler
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER