tags:

views:

78

answers:

2

Hi All,

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.

A: 

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
Kristopher Johnson
@Kristopher - I updated the post with the class where this is defined. This is all in a single source file called FxRegistry.h
MLS
+2  A: 
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. :)

UncleBens
@UncleBens - I should figure out when to use typedef and typename I dont know off the cuff.
MLS
Understanding it is good, but simple rule of thumb is to add `typename` whenever the compiler complains about something with templates.
Kristopher Johnson
@Kristopher, simple but bad rule :)
Johannes Schaub - litb