Re: Difficulty with nested template classes (newbie)
From: Cy Edmunds (cedmunds_at_spamless.rochester.rr.com)
Date: 11/23/03
- Next message: Chris \( Val \): "Re: Help with file comparison"
- Previous message: Mike Wahler: "Re: Simple ifstream question"
- In reply to: Zenon: "Difficulty with nested template classes (newbie)"
- Next in thread: Zenon: "Re: Difficulty with nested template classes (newbie)"
- Reply: Zenon: "Re: Difficulty with nested template classes (newbie)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 23 Nov 2003 02:40:02 GMT
"Zenon" <zenonk@comcast.net> wrote in message
news:b1483a18.0311221755.6a808003@posting.google.com...
> I am having difficulty with nesting templates and the proper syntax.
> Can anyone please help? I'm sure it's obvious that I'm new to C++.
> Thanks in advance.
> -Zenon
>
> template <class Type>
> class matrix
> {
> public:
>
> template <class Type>
This line is a mistake -- the template Type is already in force here. Leave
this line out.
> class iterator
> {
> int _current_row ;
> int _current_col ;
> matrix& _object ;
Should be:
matrix<Type> & _object;
You don't need to qualify "matrix" in the constructor, but you do everywhere
else.
>
> public:
> iterator (matrix& object) ;
Same here:
iterator(matrix<Type> &object);
> ~iterator ()
> {}
> iterator operator++ (int dummy) ;
> double& operator* () ;
> operator bool () const ;
> } ;
>
> // Implementation of iterator template
>
> //Constructor implementation
> template <class Type>
> matrix<Type>::iterator<Type>::iterator (matrix<Type>& object)
> : _object (object), _current_row (0), _current_col (0)
> {}
>
> // Destructor implementation
> template <class Type>
> matrix<Type>::iterator<Type>::~iterator ()
> {
> delete _object;
> }
>
> // Overloaded operator ++ implementation
> matrix<Type>::iterator matrix<Type>::iterator<Type>::operator++ (int
> dummy)
> {
> _current_col++ ;
> if (_current_col >= _object. cols())
> {
> _current_row++ ;
> _current_col = 0 ;
> }
> return *this ;
> }
>
> // Overloaded operator * implementation
> template <class Type>
> double& matrix<Type>::iterator::operator* ()
> {
> static double dummy ;
> if (*this)
> {
> return _object (_current_row, _current_col) ;
> }
> else
> {
> return dummy ;
> }
> }
>
> // Overloaded operator bool implementation
> template <class Type>
> matrix<Type>::iterator::operator bool () const
> {
> if (_current_row >= _object. rows())
> {
> return false ;
> }
> else
> {
> return true ;
> }
> }
I was too lazy to check your whole program. But I think my changes will make
it a little closer to right. Good luck.
-- Cy http://home.rochester.rr.com/cyhome/
- Next message: Chris \( Val \): "Re: Help with file comparison"
- Previous message: Mike Wahler: "Re: Simple ifstream question"
- In reply to: Zenon: "Difficulty with nested template classes (newbie)"
- Next in thread: Zenon: "Re: Difficulty with nested template classes (newbie)"
- Reply: Zenon: "Re: Difficulty with nested template classes (newbie)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]