Template question

This is a discussion on Template question within the C++ Programming forums, part of the General Programming Boards category; I started to do some reading about template, found interesting example Code: template <class T1, class T2> bool GetItUP (T1 ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    8

    Template question

    I started to do some reading about template, found interesting example
    Code:
    template <class T1, class T2>
    bool GetItUP (T1 a, T2 b) 
    {
       return (a>b?a:b);
    }
    And the way to use it:
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
       int a = 20;
       float b = 23.90;
       GetItUP<int,float>(a,b);
       return 0;
    }
    So curios enough I tried to do something like that:
    Code:
    template <typename T1, typename T2>
    void ShowMeMap(map<T1, T2>* myTempMap, string _text, bool bIsDigit)
    {
       int currentSize =  (*myTempMap).size();
       if (bIsDigit)
       {
          string temp = "AddedNewOne";
          typedef pair<int, string> ins_pair;
          (*myTempMap).insert(ins_pair(10,"NewValueInserted"));
       }
       else
       {
          //list<string> tempList ;
          //tempList.push_back("Add_1");
          //tempList.push_back("Add_2");
          //tempList.push_back("Add_3");
          //typedef pair<int, list<string>> pairTempMap;
          //(*myTempMap).insert(pairTempMap(12,&tempList));
          //(*myTempMap)[currentSize] = tempList;
       }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
       map<int,list<string>> myComplicatedMap ;
       map<int,string> myMap ;
       typedef pair<int, list<string>> pairTempMap;
    
       myMap[0] = "Starts";
       myMap[1] = "ToShow";
       myMap[2] = "Values";
    
       list<string> textList;
       textList.push_back("One");
       textList.push_back("Two");
       textList.push_back("Three");
       textList.push_back("Four");
    
       myComplicatedMap.insert(pairTempMap(1,textList));
       ShowMeMap<int,string>(&myMap, "Text_Test",true);
    // compiler error
    //ShowMeMap <int,list<string>>(&myComplicatedMap, "Text_Teste2",false);
       return 0;
    }
    Try to compile I got the following:
    error C2664: 'std::list<_Ty>::list(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'const std::string' to 'const std::allocator<_Ty> &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility
    It will be the same error if I do just like that as well:
    Code:
       ShowMeMap (&myComplicatedMap, "Text_Test2",false);
       return 0;
    }
    What Iím missing here? Can it be corrected? Is there work around (besides to make another template function)?
    Regards

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    As far as I understand you're trying to insert a pair of type <int, string> and it works fine for the first call of ShowMeMap but the second call expects <int, list<string> > however you're still inserting a pair of type <int, string>.

    I think you're over complicating this, anyway I think this could be solved with template specialization but maybe someone can give a better answer.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    8
    Thanks sugarfree,
    Yes, you understood correctly my intentions
    It might be overcomplicated, it is not the problem I'm trying to solve, this is more my curiosity
    If it is not the way to use it - fine,
    once again, thanks

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I believe it's crapping out on this line in your ShowMeMap() function:
    Code:
    (*myTempMap).insert(ins_pair(10,"NewValueInserted"));
    because you're inserting a pair<int, string>, but your map is a map<int, list<string> >

    Edit: Nevermind, I think that's what sugarfree said above.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why do you always use (*x).foo instead of x->foo? For that matter, why do you pass a pointer instead of a reference?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Btw, you don't need to specify the type...
    ShowMeMap <int,list<string>>(&myComplicatedMap, "Text_Teste2",false);
    ...can be:
    ShowMeMap(&myComplicatedMap, "Text_Teste2",false);
    The compiler will figure out the type.
    And use references instead of pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 02:52 PM
  4. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 10:43 PM
  5. templates with pointers
    By Cipher in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21