Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Error... (http://www.programmingforums.org/showthread.php?t=7282)

-=PARADOX=- Nov 29th, 2005 5:06 PM

Error...
 
Hi there again ;)

This is my new problem:

I've this template:

:

template <class T> class TArray
{
        T **m_array;
        unsigned m_nSize;
public:
        class iterator;
        friend class iterator;

        class iterator
        {
                const TArray& t;
                unsigned index;
        public:
                iterator( const TArray<T>& a, unsigned s = 0 ): t(a), index(s) {}
                const T* const operator++() { return t[++index]; }
                const T* const operator++(int) { return t[index++]; }
                const T* const operator*() const { return t[index]; };


                bool operator==( const iterator& r ) const { return ( t==r.t )? 1 : 0; }
                bool operator!=( const iterator& r ) const { return !operator==( r ); }

                friend ostream operator << (ostream& os, const iterator& it);
        };

        TArray();
        virtual ~TArray();

        void Insert (T* t);
        const T* const Max() const;

        const iterator begin() const;
        const iterator end() const;
};


Ye, ye... the definitions should be in diferent modules...

Anyway, when i do this in the main:

:

        TArray<int> ar_int;

        for ( unsigned i = 0; i < 10; i++ )
                ar_int.Insert( new int(i) );

        TArray<int>::iterator x(ar_int, 0); // this will point to the beggining

        cout << *x << endl; // this should return the first value, the error is from here


It gives me this error:

:

'operator+' not implemented in type 'TArray<int>' for arguments of type 'unsigned int'

Why the 'operator+' is needed...??? :confused:

Help here, guys...

Thanks in advance :cool:

The Dark Nov 29th, 2005 6:03 PM

When I compile it I get
Quote:

error C2676: binary '[' : 'const TArray<T>' does not define this operator or a conversion to a type acceptable to the predefined operator
What is happening is that you haven't defined a [] operator, but you are using it in your iterator code. You compiler may be trying to do &t+index or somesuch in place of t[index], leadin to the error.

Also note that your ++ operators should probably not return a different type, they should be returning the iterator, to be consistant with everyone else.

-=PARADOX=- Nov 29th, 2005 6:16 PM

Ye... duh... Thanks bro ;)


All times are GMT -5. The time now is 1:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC