Thread: Serious template issues (!!!)

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    Serious template issues (!!!)

    I'm having issues accessing template member functions of a template base class from inside a template derived class. I've provided a minimalist example that shows the problem I have.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    template <class type>
    class base {
    public:
    	base (string);
    	virtual void f (type) = 0;
    protected:
    	void g (type);
    private:
    	string name;
    };
    
    template <class type>
    base<type>::base<type> (string objectname) {
    	name = objectname;
    }
    
    template <class type>
    void base<type>::g (type value) {}
    
    template <class type>
    class derived : public base<type> {
    	public:
    	derived (string objectname) : base<type>(objectname) {};
    	
    	private:	
    	void f (type data) { base<type>::template g<type>(data); }
    
    };
    
    int main () {
    	base<int> *example;
    	string name("example");
    	example = new derived<int>(name);
    }
    This code produces the following error
    Code:
    In member function 'void derived<type>::f(type) [with type = int]'
    37: instantiated from here
    30: error: no matching function for call to 'derived<int>::g(int&)'
    I have no idea what the problem is. Any help would be greatly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    base<type>::g is a member function that belongs to a class template. It is not a function template in its own right, so to speak. Therefore, you should not be trying to disambiguate with template here:
    Code:
    base<type>::template g<type>(data);
    Rather, you should write:
    Code:
    base<type>::g(data);
    By the way, f should be declared public in derived otherwise the derived class would not behave as a subtype. Oh, and base should have a virtual destructor.
    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

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Thanks! You're a life-saver.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Template issues
    By Frobozz in forum C++ Programming
    Replies: 6
    Last Post: 08-26-2007, 01:13 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 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