[opensuse-programming] Question on C++ and STL
I have a template class that contains a container:
template <class T>
class MyClass
{
:
private:
std::list<T> ilist;
};
I have no problem accessing the STL container using subscript,
but I am unable to declare an iterator, as an example:
void MyClass<T>::erase(size_t n)
{
std::list<T>::iterator it;
...
g++ gives me an error:
containertest.cc: In member function ‘void MyClass<T>::erase(size_t)’:
containertest.cc:13: error: expected `;' before ‘it’
Note: I've included both <list> and <iterator>.
Now if I declare something like:
std::list<double>::iterator it;
or
std::liststd::string::iterator it;
Everything builds.
One possible solution is to inherit the list, but I wanted to avoid
inheriting STL containers.
--
Jerry Feldman
Am Sonntag, 21. Januar 2007 17:57 schrieb Jerry Feldman:
containertest.cc: In member function ‘void MyClass<T>::erase(size_t)’: containertest.cc:13: error: expected `;' before ‘it’ Note: I've included both <list> and <iterator>.
You must define that method in the header-file. The compiler can't generate code unless it knows the actual type...
Now if I declare something like: std::list<double>::iterator it; or std::liststd::string::iterator it;
Everything builds. ...like here.
To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
Am Sonntag, 21. Januar 2007 17:57 schrieb Jerry Feldman:
containertest.cc: In member function ‘void MyClass<T>::erase(size_t)’: containertest.cc:13: error: expected `;' before ‘it’ Note: I've included both <list> and <iterator>.
You must define that method in the header-file. The compiler can't generate code unless it knows the actual type...
Now if I declare something like: std::list<double>::iterator it; or std::liststd::string::iterator it;
Everything builds. ...like here. The problem is that there is a template class. I certainly can define MyClass so that the internal container is a list (or vector) of string or int. But, the real issue is to be able to use an STL container class within a template class. So, that we won't know what the container holds until it is instantiated. I certainly can build a MyClassString and MyClassDouble, but that takes away from the beauty of template classes. In the actual code, I actually have a 2 dimensional vector where I wanted to use an STL class rather than the previously existing home-grown classes. For the implementation as I posted it in my post,
On Sun, 21 Jan 2007 20:57:43 +0100
Volker Poplawski
participants (2)
-
Jerry Feldman
-
Volker Poplawski