0

私のヘッダーファイルには

template <typename T>
class Vector {
    public:
         // constructor and other things

         const Vector& operator=(const Vector &rhs);   
};

ここに私がこれまでに試した1つの宣言があります

template <typename T> Vector& Vector< T >::operator=( const Vector &rhs )
{
    if( this != &rhs )
    {
        delete [ ] array;
        theSize = rhs.size();
        theCapacity = rhs.capacity();

        array = new T[ capacity() ];
        for( int i = 0; i < size(); i++ ){
            array[ i ] = rhs.array[ i ];
        }
    }
    return *this;
}

これはコンパイラが私に言っていることです

In file included from Vector.h:96,
                 from main.cpp:2:
Vector.cpp:18: error: expected constructor, destructor, or type conversion before ‘&amp;’ token
make: *** [project1] Error 1

コピーコンストラクターを正しく宣言するにはどうすればよいですか?

注: これはプロジェクト用であり、ヘッダー宣言を変更できないため、このような提案は便利ですが、この特定のインスタンスでは役に立ちません。

助けてくれてありがとう!

4

1 に答える 1

2

注: コピー コンストラクターではなく、代入演算子を宣言します。

  1. const戻り型の前に修飾子がありませんでした
  2. <T>戻り値の型と関数の引数のテンプレート引数( )がありません

これを使って:

template <typename T>
const Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
于 2013-02-12T01:41:59.390 に答える