开发者

Getting error: no match for operator <<, when using operator ++ concurrently

I'm trying to overload many operators for a class assignment and need to get the << and ++ operators working together. Below is a sample of the code I'm dealing with. Let me know if you have any ideas.

decaring generic template

template (class T)  
class Vector 
{  
    public: 

    class VectIter
    {
        friend class Vector;
        private:
            Vector<T> *v; // points to a vector object of type T
            int index;    // represents the subscript number of the vector's
            // array.
        public:
            VectIter(Vector<T>& x);
            T operator++();
            T operator++(int);
            T operator--();
            T operator--(int);
            T operator *();

            friend ostream& operator <<(ostream& out, const VectIter& rhs)
            {
                out << (*rhs) <<endl;
                return out;
            }
    };

    Vector(int sz); 
    ~Vector();

    T & operator[](int i); 
    void ascending_sort();

    private:
        T *array;              // points to the first element of an array of T
        int size;
        void swap(T&, T&);
};

and here is where the error occurs in the main:

Vector<Mystring> y(3);
y[0] = "Bar";
y[1] 开发者_运维问答= "Foo";
y[2] = "All";;

Vector<Mystring>::VectIter iters(y);

cout << "\n\nTesting Postfix --";
for (int i=0; i<3 ; i++)
    cout << endl << (iters++);

Here is a sample of the operators I'm using:

T Vector<T>::VectIter::operator ++()  
{  
    if(index == (*v).size)  
        index = 0;  
    else  
        index++;  
    return (*v).array[index];  
}

T Vector<T>::VectIter::operator ++(int post)  
{  
    post = index;  
    if(index == (*v).size)  
        index = 0;  
    else  
        index++;  
    return (*v).array[post];  
}

This code seems to work with int variables but when I change it to my custom class Mystring I get the error.


Your iterator operator++ returns a T rather than an iterator, so your iterator's operator<< isn't getting called. Most likely Mystring doesn't have an operator<< declared/defined OR Mystring's operator<< takes a non-const reference as its second parameter and can't accept the temporary returned from your operator++.

EDIT: Given your update to the OP (that it works with int) you almost certainly need to implement operator<< for your Mystring class.


Hmm...you've defined your iterators quite a bit differently than they're normally defined. An iterator (at least in C++) normally acts roughly like a pointer: when you increment it or decrement it, the result is an iterator, not whatever type the iterator refers to. You have to dereference the iterator to get the referred to type.

The error you're seeing looks like it stems from the fact that although you've declared the ++ and -- operators, you haven't defined them -- so when you try to use them, your code won't compile/link any more.


Does your Mystring class have a << operator defined? Your iterator will end up exposing a Mystring object, whose operator<<() would be called with an ostream; you might be getting this error because it is not defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜