1
votes

I have a problem with the container map. I need to store my own class Person in key but i have error C2784 (i.e., "The compiler cannot determine a template argument from the supplied function arguments."). It's example from the book "Ivor Horton's beginning Visual C++ 2010"

#include<map>
#include<string>
#include <iostream>
using namespace std;

void main()
{
    class Person{
     public:
     string c_name,c_surname;
     Person(string name,string surname){
     c_name=name;
     c_surname=surname;
        }
    };

     map<Person,string> phonebook;

     phonebook.insert(make_pair(Person("Mel","GIBSON"),"24 32 23"));
     phonebook[Person("Mel2","Gibson2")]="243 32 23";

      /* it doesn`t work too
     typedef pair<Person,string> Entry;
     Entry entry1= Entry(Person("Jack","Jones"),"213 567 1234");

     phonebook.insert(entry1);*/

    system("Pause");
}

Error 1 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 2 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 3 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 4 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 5 error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 6 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 7 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 8 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

Error 9 error C2676: binary '<' : 'const main::Person' does not define this operator or a conversion to a type acceptable to the predefined operator e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

2
For those of us that doesn't know what "error C2784" is, please copy the complete and unedited error log into your question. In fact, when asking questions about any compiler or linker error, please always do that.Some programmer dude
one part of the error is due to this (local type used in templates): stackoverflow.com/questions/5751977/… Fix it by moving definition of Person outside main.Manoj Awasthi

2 Answers

1
votes

The problem here is that std::map requires your keys to be comparable with the < operator. Custom structures/classes are not that by default, you need to make a custom operator< for comparison.

1
votes

In C++03 you could not use local classes (classes defined within functions) as template arguments.

In C++11 you can.

So one fix is to update the compiler (there is Visual C++ 2013), and another fix is to move the class definition out of main.


By the way, void main is invalid as standard C++, and as standard C, and it's more to type than standard int main. If your book has void main, then that's a very ungood book. Microsoft's examples that include void main are also very ungood.


Also, by the way, the

system("Pause");

at the end is also very ungood practice because

  • it is not necessary, has no advantage, but

  • it makes the program more difficult to use and has some other problems, and to top it all,

  • it's Windows-specific, non-portable code.

To run a console program so that it stops at the end

  • in Visual Studio use Ctrl+F5, or

  • in Visual Studio place a breakpoint at the end of main (just click in the left margin) and run it with debugging (e.g. via keypress F5), or

  • run it from a command interpreter.


UPDATE: the now added error messages (even the first one) mention operator<. You need to define that also. That is, define an operator< function for your class Person.