Thread: Template-Related Problems

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    28

    Question Template-Related Problems

    I'm having a problem with using templates, and since I'm completely at a loss as to what's wrong, I'm simply going to dump the (I think) relevant sections of code here.

    Here is the problem code:

    The bolded line is the one throwing the error.
    Code:
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    template<class client_type> class ServiceList {
    public:
        /* blah blah blah... */
    
        // move a client off the to-serve list
        void signOut(client_type & targ) { signOut(&targ); };
        void signOut(client_type * const targ) {
            // find the given client pointer in our list
            vector<client_type *>::iterator where =
                find(client_list.begin(),client_list.end(), targ);
    
            // if it's in there, delete it!
            if (where != client_list.end())
                    client_list.erase(where);
        };
    
         /* more blah... */
    protected:
        // the list of pointers to clients
        vector<client_type *> client_list;
    };
    Here is the compiler error:
    Code:
    In file included from KeyDispatcher.hpp:7,
                     from ProtPlay.hpp:4,
                     from Follower.hpp:4,
                     from Follower.cpp:1:
    ServiceList.hpp: In member function `void ServiceList<client_type>::signOut(client_type*)':
    ServiceList.hpp:26: error: expected `;' before "where"
    ServiceList.hpp:30: error: `where' undeclared (first use this function)
    ServiceList.hpp:30: error: (Each undeclared identifier is reported only once for each function it appears in.)
    The bolded line refers to the bolded bit in the code segment I posted.

    By the way, I'm using Dev-C++ 4.9.9.2.

    As I said, I don't know what's wrong at all. Help me, please?

    And if you have some clever suggestions about the implementation in general, feel free to state them. This class is a template class to use in creating a message-dispatching class. Specifically, I'm currently using it to dispatch keyboard messages to a bunch of character queues. The class keeps a vector of pointers to the client character queues, and the child class of ServiceList will specify a function to actually dispatch the messages by iterating through the vector. I decided on a vector because I don't care (at least, not now) about signin/signout speeds, just the time it takes to dispatch a message.

    So, yeah. You.Help(Me); ?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Try this:
    Code:
            typename vector<client_type *>::iterator where =
                find(client_list.begin(),client_list.end(), targ);
    gg

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    28
    Thanks. It worked and is glorious.

    So, could someone explain to me why typename was required?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Whenever you use a name that is dependent on a template parameter, you have to qualify the name using the typename keyword.

    So in your example, the complete type name of "vector<client_type *>::iterator" is dependent on the the template parameter client_type and therefore needs to be qualified using typename.

    More details:
    http://www.gotw.ca/gotw/035.htm

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template overloading?
    By cpjust in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2008, 03:21 PM
  2. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 01:46 AM
  3. Replies: 2
    Last Post: 05-29-2005, 10:06 AM
  4. More template problems
    By VirtualAce in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2002, 06:16 PM
  5. Template problems
    By VirtualAce in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 11:11 PM