MLS
MLS

Reputation: 645

Templates --> How to decipher, decide if necessary and create?

I have a few classes in a project that I inherited that are really old, last I knew they compiled with CodeWarrior 8. I am now in XCode 3.2

Here is an example of what I struggle with:

template <class registeredObject> 
typename std::vector<registeredObject>::iterator FxRegistry<registeredObject>::begin(void)
{
    return mRegistryList.begin();
}

The errors are:

no 'typename std::vector<registeredObject, std::allocator<_CharT> >::iterator FxRegistry<registeredObject>::begin()' member function declared in class 'FxRegistry<registeredObject>'

template definition of non-template 'typename std::vector<registeredObject, std::allocator<_CharT> >::iterator FxRegistry<registeredObject>::begin()'

How do I decide how to solve these and where do I start looking?

UPDATE: Here is the FxRegistry Class:

template <class registeredObject>
class FxRegistry
{
public:
    // Construction
    FxRegistry() {}
    ~FxRegistry();

    // List management
    void Register(const registeredObject &ob);
    void Unregister(registeredObject &ob);

    // List iteration
    typedef std::vector<registeredObject>::iterator iterator;
    typedef std::vector<registeredObject>::const_iterator const_iterator;
    std::vector<registeredObject>::iterator begin(void);
    std::vector<registeredObject>::const_iterator begin(void) const;
    std::vector<registeredObject>::iterator end(void);
        std::vector<registeredObject>::const_iterator end(void) const;

    FxSize size(void);
    void Insert(iterator iter,const registeredObject &ob);
    registeredObject &operator[](FxInt32 index) { return mRegistryList[index]; }
        void clear() {mRegistryList.clear();};
protected:
    vector<registeredObject> mRegistryList;
};

I get errors on every line above almost that are: error: type 'std::vector >' is not derived from type 'FxRegistry' error: expected ';' before 'iterator'

I thought an iterator was of vector type, so I would declare one vector iterator.

Upvotes: 0

Views: 165

Answers (2)

Kristopher Johnson
Kristopher Johnson

Reputation: 82535

Check the following:

  • Is there an #include <vector> before the inclusion of that header?
  • In the protected part of the class declaration, vector is not qualified with std::.
  • Add typename before each of the std::vector<registeredObject>::iterator and ...const_iterator uses

Upvotes: 0

UncleBens
UncleBens

Reputation: 41331

typedef std::vector<registeredObject>::iterator iterator;
typedef std::vector<registeredObject>::const_iterator const_iterator;
std::vector<registeredObject>::iterator begin(void);
std::vector<registeredObject>::const_iterator begin(void) const;
std::vector<registeredObject>::iterator end(void);
    std::vector<registeredObject>::const_iterator end(void) const;

In all those places you should be using:

typename std::vector<registeredObject>::iterator

iterator and const_iterator are dependent names (their meaning depends on what a particular instantiation of vector<X> might turn out to contain), and the language requires that you tell whether it is a type name or not, so the compiler can know without instantiating vector<X>.

Also it might make sense to use the typedefs you define.

As to what to start from - the first error on the list. :)

Upvotes: 2

Related Questions