Like Tree1Likes
  • 1 Post By gardhr

Problem with templates

This is a discussion on Problem with templates within the C++ Programming forums, part of the General Programming Boards category; Suppose I want a function to work on 'most' types of containers having 'most' types of data. ('most' because I'll ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505

    Problem with templates

    Suppose I want a function to work on 'most' types of containers having 'most' types of data. ('most' because I'll assume some similar operations are permitted.)

    But the following definition doesn't seem to be allowed.
    Code:
    template<typename List,typename Dat>
    List<Dat>::iterator search(List<Dat> input, Dat key)
    {
         //....   
    }
    (I know that searching isn't a good problem to have a similar solution for all types of lists but this is just for the example.)

    The error clearly says that List isn't a template.

    How do I do what I want to... ? (if possible at all).
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Tried to do with
    Code:
        template<typename List,typename Dat>
        List::iterator search(List input,Dat key)
        {
            
        }
    But now there is another error:
    Quote Originally Posted by g++
    a.cpp:7:5: error: need ‘typename’ before ‘List::iterator’ because ‘List’ is a dependent scope
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Hence you use typename List::iterator as the return type.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Wow...
    Being used to cryptic error messages, I did not even bother to check its literal meaning !
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    I know what you mean. I chuckled when I saw that the error message even explicitly told you what to do yet you ignored it, presumably because your brain turned off the moment you saw that it was a template error
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Yeah, we really need a template for interpreting template-related errors.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by manasij7479 View Post
    Suppose I want a function to work on 'most' types of containers having 'most' types of data. ('most' because I'll assume some similar operations are permitted.)

    But the following definition doesn't seem to be allowed.
    Code:
    template<typename List,typename Dat>
    List<Dat>::iterator search(List<Dat> input, Dat key)
    {
         //....   
    }
    (I know that searching isn't a good problem to have a similar solution for all types of lists but this is just for the example.)

    The error clearly says that List isn't a template.

    How do I do what I want to... ? (if possible at all).
    Just a suggestion: if you really want it to work with as many data types as possible, keep it generic.

    Code:
    template <typename Container>
    typename Container::const_iterator search(const Container& input, const typename Container::value_type& key)
    {
    	return std::find(input.begin(), input.end(), key);
    }
    manasij7479 likes this.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    @gardhr:
    Well, my intention was to make a sandbox for implementing the search algorithms as I learn them.

    Now I realize that it is much simpler to just take the begin and end iterators.. and the key (and maybe a functor/or pointer to extract the key from the respective record objects.. which would be an identity function by default.) as the arguments to follow the design of the <algorithm>s in general,
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with templates
    By valery in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2009, 04:12 PM
  2. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 05:32 PM
  3. Problem with templates
    By ThWolf in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2006, 02:07 PM
  4. problem with templates
    By KRNLfuxx0r in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2006, 02:20 PM
  5. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 12:57 PM

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