Patrizio Bertoni Patrizio Bertoni - 3 months ago 11
C++ Question

Template typedef is not a class or namespace name

I'm writing a template class which should behave like a container. Inner data is a vector of smart pointers to the generic class

T
.

Header



#include <vector>
#include <boost/shared_ptr.hpp>

namespace Foo
{
template <class T>
class AstrContainer
{

public:

typedef boost::shared_ptr<T> p_element;
typedef std::vector<p_element> v_p_elements;

protected:

v_p_elements _vData;

public:
AstrContainer();
virtual ~AstrContainer();

typename v_p_elements::iterator begin();
typename v_p_elements::iterator end();
};
}


Source



#include "AstrContainer.hpp"

namespace Foo
{
template<class T>
AstrContainer<T>::AstrContainer()
{
}

template<class T>
AstrContainer<T>::~AstrContainer()
{
}

typename
v_p_elements::iterator AstrContainer<T>::begin() // - - - ERROR LINE 1 - - -
{
return _vData.begin();
}

template<class T>
typename v_p_elements::iterator AstrContainer<T>::end() // - - - ERROR LINE 2 - - -
{
return _vData.end();
}
}


I'm very new to template classes in C++, and got a bit stucked at the ERROR LINE 1


error C2653: 'v_p_elements': is not a class or namespace name


So I commented the
begin()
method, but at ERROR LINE 2 it halts with the same error.

Now it seems clear that, since
v_p_elements
is being typedef'd inside the class, that maybe it cannot be exported to the outer world. But now I'm asking if the whole thing is even possible, or if I'm just misunderstanding something.

Answer

You could add qualified enclosing class name.

template<class T>
typename AstrContainer<T>::v_p_elements::iterator AstrContainer<T>::end()
//       ~~~~~~~~~~~~~~~~~~
{
    return _vData.end();
}
Comments