A C2664 error

Discussion in 'C++' started by zhxzhx@gmail.com, Dec 21, 2007.

  1. Guest

    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.
     
    , Dec 21, 2007
    #1
    1. Advertising

  2. Ondra Holub Guest

    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";
    }
    }
     
    Ondra Holub, Dec 21, 2007
    #2
    1. Advertising

  3. Guest

    On 12ÔÂ21ÈÕ, ÏÂÎç4ʱ23·Ö, Ondra Holub <> wrote:
    > 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.
     
    , Dec 21, 2007
    #3
  4. Guest

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

    Thank you very much :)
     
    , Dec 21, 2007
    #4
  5. 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 ()
    Any fool can tell the truth, but it requires a man of sense to know
    how to lie well.
    -- Samuel Butler
     
    Tadeusz B. Kopec, Dec 21, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison
    Replies:
    1
    Views:
    15,011
    Tim Mierzejewski
    Jun 25, 2003
  2. Suzanne
    Replies:
    0
    Views:
    1,043
    Suzanne
    Jun 25, 2003
  3. Jack Klein
    Replies:
    0
    Views:
    2,727
    Jack Klein
    Jun 25, 2003
  4. David Hoffman

    Compile error C2664 - Why??

    David Hoffman, Oct 11, 2003, in forum: C++
    Replies:
    4
    Views:
    3,718
    David Hoffman
    Oct 12, 2003
  5. mkarja

    Compiler error C2664

    mkarja, Feb 6, 2005, in forum: C++
    Replies:
    1
    Views:
    1,700
    David Lindauer
    Feb 6, 2005
Loading...

Share This Page