template specialization

This is a discussion on template specialization within the C++ Programming forums, part of the General Programming Boards category; I wanted to specify two very, very similar classes without having to write them out twice. However, when I specify ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907

    template specialization

    I wanted to specify two very, very similar classes without having to write them out twice. However, when I specify member functions of one of the two possible cases, the compiler complains that the type I'm referring to is incomplete. What am I missing in this method? (stripped down)
    Code:
    template<class unary_func, bool Kd_weighted = false>
    class sum : public func2d
    {
    public:
        void operator()(grid::size_type x, grid::size_type y, grid::base_type & g, grid::data_type Kd = 1.0);
    private:
        grid::data_type s;
        unary_func f;
    };
    
    template<class unary_func>
    void sum<unary_func,false>::operator()(grid::size_type x, grid::size_type y, grid::base_type & g, grid::data_type Kd)
    {
            s += f( g(x,y) );
    }
    
    template<class unary_func>
    void sum<unary_func,true>::operator()(grid::size_type x, grid::size_type y, grid::base_type & g, grid::data_type Kd)
    {
            s += Kd * f( g(x,y) );
    }
    Code:
    error: invalid use of incomplete type ‘class functions::sum<unary_func, false>’|
    error: declaration of ‘class functions::sum<unary_func, false>’|
    error: invalid use of incomplete type ‘class functions::sum<unary_func, true>’|
    error: declaration of ‘class functions::sum<unary_func, true>’|
    Thanks.

    *edit* Of course the classes are incomplete, but won't they be written at compile time, since I'm using them? Does the 'bool' not limit us to only two possibilities for a given unary_func?
    Last edited by CodeMonkey; 12-28-2008 at 11:41 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    You can't specialize a member function of a class template. You have to specialize the class template itself.

    gg

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    Well would you look at that.

    Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It might look something like that:

    Code:
    template <bool Kd_weighted>
    struct summer {};
    
    template <>
    struct summer<true>
    {
        template <class unary_func>
        grid::data_type operator()(grid::size_type x, grid::size_type y, grid::base_type& g, grid::data_type Kd, unary_func f)
        {
            return Kd * f( g(x, y) );
        }
    };
    
    template <>
    struct summer<false>
    {
        template <class unary_func>
        grid::data_type operator()(grid::size_type x, grid::size_type y, grid::base_type& g, grid::data_type, unary_func f)
        {
            return f( g(x, y) );
        }
    };
    
    template<class unary_func, bool Kd_weighted>
    void sum<unary_func,Kd_weighted>::operator()(grid::size_type x, grid::size_type y, grid::base_type & g, grid::data_type Kd)
    {
            s += summer<Kd_weighted>()(x, y, g, Kd, f);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Template specialization
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 02:02 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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