C++ Template Question

S

Steven Green

Hi Guys, I've been away from C++ since 2001 (been playing in Java land) but
I am trying to refresh my grey cells with C++.
What I am trying to do is wrap a std::list<T*> with a simple wrapper.

template <class T>
class Adapter
{
private:
std::list<T*> data;
public:
void remove(T*);
}

void Adapter<T>::remove(T *d)
{
std::list<T*>::iterator i = data.begin(); //the offencive line
}

Pardon me if there are any syntax errors in the above code, I am typing this
not cut and pasting.

The problem:
In th above code I create an iterator of type T*. This doesn't work it wants
the T* to be a class type:
For example:
std::list<Widget>::iterator i = data.begin(); //works
std::list<T*>::iterator i = data.begin(); //does not work.

I don't doubt that I am doing something dumb, but I cannot find my C++ book
(it is neatly tucked away under a pile somewhere to be sure)
Any thoughts on this would be appreciated.
Thanks,
Steve
 
M

mlimber

Steven said:
Hi Guys, I've been away from C++ since 2001 (been playing in Java land) but
I am trying to refresh my grey cells with C++.
What I am trying to do is wrap a std::list<T*> with a simple wrapper.

template <class T>
class Adapter
{
private:
std::list<T*> data;
public:
void remove(T*);
}

Could your problem be that you are missing this line:

template said:
void Adapter<T>::remove(T *d)
{
std::list<T*>::iterator i = data.begin(); //the offencive line
}

Pardon me if there are any syntax errors in the above code, I am typing this
not cut and pasting.

The problem:
In th above code I create an iterator of type T*. This doesn't work it wants
the T* to be a class type:
For example:
std::list<Widget>::iterator i = data.begin(); //works
std::list<T*>::iterator i = data.begin(); //does not work.

I don't doubt that I am doing something dumb, but I cannot find my C++ book
(it is neatly tucked away under a pile somewhere to be sure)
Any thoughts on this would be appreciated.
Thanks,
Steve

If that's not it, please cut and paste a complete but minimal sample
that demonstrates the problem.

Cheers! --M
 
B

Bob Hairgrove

Hi Guys, I've been away from C++ since 2001 (been playing in Java land) but
I am trying to refresh my grey cells with C++.
What I am trying to do is wrap a std::list<T*> with a simple wrapper.

template <class T>
class Adapter
{
private:
std::list<T*> data;
public:
void remove(T*);
}

void Adapter<T>::remove(T *d)
{
std::list<T*>::iterator i = data.begin(); //the offencive line
}

Pardon me if there are any syntax errors in the above code, I am typing this
not cut and pasting.

The problem:
In th above code I create an iterator of type T*. This doesn't work it wants
the T* to be a class type:
For example:
std::list<Widget>::iterator i = data.begin(); //works
std::list<T*>::iterator i = data.begin(); //does not work.

I don't doubt that I am doing something dumb, but I cannot find my C++ book
(it is neatly tucked away under a pile somewhere to be sure)
Any thoughts on this would be appreciated.
Thanks,
Steve

Try this:

void Adapter<T>::remove(T *d)
{
typename std::list<T*>::iterator i = data.begin();
// ^^^^^^^^
}

You need the keyword "typename" because the iterator's type depends on
the template parameter. If you specify a real type such as Widget, the
compiler can resolve it without the typename.

Implicit typename has been deprecated for 2 or 3 years now, so maybe
it used to work in 2001.
 
G

Greg

Steven said:
Hi Guys, I've been away from C++ since 2001 (been playing in Java land) but
I am trying to refresh my grey cells with C++.
What I am trying to do is wrap a std::list<T*> with a simple wrapper.

template <class T>
class Adapter
{
private:
std::list<T*> data;
public:
void remove(T*);
}

void Adapter<T>::remove(T *d)
{
std::list<T*>::iterator i = data.begin(); //the offencive line
}

Pardon me if there are any syntax errors in the above code, I am typing this
not cut and pasting.

The problem:
In th above code I create an iterator of type T*. This doesn't work it wants
the T* to be a class type:
For example:
std::list<Widget>::iterator i = data.begin(); //works
std::list<T*>::iterator i = data.begin(); //does not work.

I don't doubt that I am doing something dumb, but I cannot find my C++ book
(it is neatly tucked away under a pile somewhere to be sure)
Any thoughts on this would be appreciated.

Try:

typename std::list<T*>::iterator i = data.begin();

Greg
 
J

John Carson

Bob Hairgrove said:
Try this:

void Adapter<T>::remove(T *d)
{
typename std::list<T*>::iterator i = data.begin();
// ^^^^^^^^
}


Even better, try this:

template<class T>
void Adapter<T>::remove(T *d)
{
typename std::list<T*>::iterator i = data.begin();
// ^^^^^^^^
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,223
Latest member
Jurgen2087

Latest Threads

Top