Thread: Problem with templates

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

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

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hence you use typename List::iterator as the return type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Wow...
    Being used to cryptic error messages, I did not even bother to check its literal meaning !

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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 or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, 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);
    }

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    @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,

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, 03:12 PM
  2. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  3. Problem with templates
    By ThWolf in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2006, 01:07 PM
  4. problem with templates
    By KRNLfuxx0r in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2006, 01:20 PM
  5. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 12:57 PM