Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A C2664 error

Reply
Thread Tools

A C2664 error

 
 
zhxzhx@gmail.com
Guest
Posts: n/a
 
      12-21-2007
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.
 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      12-21-2007


(E-Mail Removed) 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";
}
}
 
Reply With Quote
 
 
 
 
zhxzhx@gmail.com
Guest
Posts: n/a
 
      12-21-2007
On 12月21日, 下午4时23分, Ondra Holub <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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";
> }
>
>
>
> }


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.
 
Reply With Quote
 
zhxzhx@gmail.com
Guest
Posts: n/a
 
      12-21-2007
Oh I think I make a mistake, it's should not be &mwdic() but mwdic.

Thank you very much
 
Reply With Quote
 
Tadeusz B. Kopec
Guest
Posts: n/a
 
      12-21-2007
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 ((E-Mail Removed))
Any fool can tell the truth, but it requires a man of sense to know
how to lie well.
-- Samuel Butler
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler error C2664 mkarja C++ 1 02-06-2005 10:28 PM
Compile error C2664 - Why?? David Hoffman C++ 4 10-12-2003 02:07 AM
Re: Error C2664: Cannot Convert Parameter 1 from 'x' to 'y' John Harrison C++ 1 06-25-2003 08:03 PM
Re: Error C2664: Cannot Convert Parameter 1 from 'x' to 'y' Jack Klein C++ 0 06-25-2003 05:39 PM
Re: Error C2664: Cannot Convert Parameter 1 from 'x' to 'y' Suzanne C++ 0 06-25-2003 05:38 PM



Advertisments