A C2664 error

Z

zhxzhx

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

Ondra Holub

(e-mail address 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";
}
}
 
Z

zhxzhx

(e-mail address 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.
 
Z

zhxzhx

Oh I think I make a mistake, it's should not be &mwdic() but mwdic.

Thank you very much :)
 
T

Tadeusz B. Kopec

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.
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top