Maybe it's a really basic question.....
..but why does this NOT work?

Respectively what options do I have to get this working?

g++ reports:
expected constructor, destructor, or type conversion before ...
(I marked the line below in which the error occured.)

Your help is very appreciated!
Thanks!

#ifndef SAMPLE_H_
#define SAMPLE_H_

#include <iterator>
#include <vector>

template <typename T>
class Sample {
private:
	typedef std::vector<T> vectorType;
	vectorType vector;

public:
	typedef typename vectorType::iterator vectorIterator;
	vectorIterator begin();
};
#endif


	template <typename T>
	vectorIterator Sample<T>::begin() {    <== ERROR HERE 
		return vector.begin();
	}

Recommended Answers

All 3 Replies

It works:

template <typename T>
class Sample {
private:
	typedef std::vector<T> vectorType;
	vectorType vector;

public:
	typedef typename vectorType::iterator vectorIterator;
	vectorIterator begin();
};

template <typename T>
typename Sample<T>::vectorIterator Sample<T>::begin() {
	return vector.begin();
}

An example from the C++ standard:

template<class T> struct A {
  typedef int B;
  A::B b; // ill-formed: typename required before A::B
  void f(A<T>::B); // ill-formed: typename required before A<T>::B
  typename A::B g(); // OK
};

The keyword typename is required whether the qualified name is A or A<T> because A or A<T> are synonyms
within a class template with the parameter list <T>.

Problem solved! :-)

Sometimes there are only the little things that cause the errors, like in my code the missed typename before the return value.

I believed that I could use the keyword typename only within a class.

@ArkM: I thought it wouldn't be that complicate; actually I tried your submitted code, but only without the typdef in front and therefore I never got it working even after hours with google... ;-D

Maybe I'm simply not that c++ coder ;-)


@all of you:
Thank you very, very much for your cooperation!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.