如何重载<<运营商?

时间:2015-03-24 14:43:21

标签: c++ operator-overloading operator-keyword cout

我正在尝试构建自己的类矢量类,我想重载下标([])和cout(&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;我想以某种方式写cout<<A ,其中A是MyVector类型。 此外,我试图重载+运算符,所以当我调用A + B时​​,它连接两个数组的内容(即[2,3,4] + [4,5,6] = [2,3,4,但是,当我尝试编译它时,会给出大约50行错误,这些错误属于iostream库。我试图将cout重载定义为公共成员,然后在类声明之外编写函数体所以它会做我期望它做的事情,但似乎我错了。任何帮助?我做错了什么?

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
template<typename T>
class MyVector{

private:
    T* Array;
    int Dimension;
    int Capacity;
public:


    MyVector()
    {
        this->Capacity=10;
        this->Dimension=0;
        Array = new T[10];
    }
    MyVector(int L)
    {
        this->Capacity=L;
        this->Dimension=0;
        Array=new T[L];
    }
    void add(T Element)
    {
        if((Dimension+1)>Capacity)
        {
            cout<<"Dimension exceeds Capacity, reallocating array\n";
            Array=(T*)realloc(Array,2*Capacity);
            Dimension++;
            Array[Dimension]=Element;
        }
        else
        {
            Array[Dimension]=Element;
            Dimension++;

        }
    }
    void remove(int Position)
    {
        int i;
        if (Position==Dimension-1)
        {
            if ((Dimension-1)<Capacity/2)
            {
                Dimension--;
                Capacity/=2;
                Array=(T*)realloc(Array,Capacity);
            }

        }
        else
        {
            if((Dimension-1)<Capacity/2)
            {
                for(i=Position+1;i<Dimension;i++)
                {
                    Array[i-1]=Array[i];
                }
                Dimension--;
                Capacity/=2;
                Array=(T*)realloc(Array,Capacity);
            }
        }
    }
    void removeElement(T Element)
    {
        int i;
        for(i=0;i<Dimension;i++)
        {
            if (Array[i]==Element)
            {
                this->remove(i);
                return;
            }
        }
        cout<<"Error: Element not found !\n";
    }
    T& operator[](int b)
    {
        if ((b>=0)&&(b<this->Dimension))
        {
             return *(Array+b);
        }
        else
        {
            cout<<"Index exceeds array range!\n";
        }
    }
    MyVector operator+(MyVector list2)
    {
        int i;
        MyVector A=MyVector(this->Capacity+list2.Capacity);
        A.Dimension=this->Dimension+list2.Dimension;
        for(i=0;i<A.Dimension;i++)
        {
            if (i<this->Dimension)
            {
                A.add(this->Array[i]);
            }
            else
            {
                A.add(list2.Array[i]);
            }
        }
    }
    T* begin()
    {
        return *Array;
    }
    T* end()
    {
        return *(Array+Dimension);
    }
    int GetCapacity()
    {
        return this->Capacity;
    }
    int GetDimension()
    {
        return this->Dimension;
    }
    std::ostream& output(std::ostream& s) const;
    ~MyVector(){}
    };
std::ostream& operator<<(std::ostream& output,MyVector& A)
    {
        int i;
        for(i=0;i<A.GetDimension();i++)
        {
           output<<A[i]<<" ";
        }
        output<<"\n";
        return output;
    };
 int main()
 {
int i;
MyVector<int> A;
for(i=0;i<5;i++)
{
    A.add(i*2);
    cout<<A[i];
}


return 0;
}

2 个答案:

答案 0 :(得分:0)

你拥有的是对的,你忘记了模板声明:

template <typename T>
std::ostream& operator<<(std::ostream& output, MyVector<T>& A)
{
    int i;
    for (i = 0; i < A.GetDimension(); i++)
    {
        output << A[i] << " ";
    }
    output << "\n";
    return output;
};

答案 1 :(得分:0)

template<typename T>
class MyVector{
    ...
    std::ostream& printToStream(std::ostream& output) const {
        int i;
        for (i = 0; i < Dimension; i++) {
            output << Array[i] << " ";
        }
        output << "\n";
        return output;
    }
    ...
};

template<typename T>
std::ostream& operator<<(std::ostream& output, const MyVector<T>& A) {
   return A.printToStream(output);
}