0

私は次のクラスを持っています

#ifndef Container_H
#define Container_H
#include <iostream>
using namespace std;

class Container{

    friend bool operator==(const Container &rhs,const Container &lhs);
 public:
   void display(ostream & out) const;

 private:
   int sizeC;                // size of Container
   int capacityC;            // capacity of dynamic array
   int * elements;            // pntr to dynamic array
  };
ostream & operator<< (ostream & out, const Container & aCont);
#endif

そしてこのソースファイル

#include "container.h"

/*----------------------------*********************************************
note: to test whether capacityC and sizeC are equal, must i add 1 to sizeC?
seeing as sizeC starts off with 0??
*/

Container::Container(int maxCapacity){
    capacityC = maxCapacity;
    elements = new int [capacityC];
    sizeC = 0;
}

Container::~Container(){
    delete [] elements;
}

Container::Container(const Container & origCont){
    //copy constructor?
    int i = 0;
    for (i = 0; i<capacityC; i++){ //capacity to be used here?
        (*this).elements[i] = origCont.elements[i];

    }
}

bool Container::empty() const{
    if (sizeC == 0){
        return true;
    }else{
        return false;
    }
}

void Container::insert(int item, int index){
    if ( sizeC == capacityC ){
        cout << "\n*** Next:  Bye!\n";
        return; // ? have return here?
    }
    if ( (index >= 0) && (index <= capacityC) ){
        elements[index] = item;
        sizeC++;
    }
    if ( (index < 0) && (index > capacityC) ){
        cout<<"*** Illegal location to insert--"<< index << ". Container unchanged. ***\n";
    }//error here not valid? according to original a3? have i implemented wrong?
}

void Container::erase(int index){
    if ( (index >= 0) && (index <= capacityC) ){ //correct here? legal location?
        int i = 0;
        while (i<capacityC){ //correct?
            elements[index] = elements[index+1]; //check if index increases here.
            i++;
        }
        sizeC=sizeC-1; //correct? updated sizeC?
    }else{
        cout<<"*** Illegal location to be removed--"<< index << ". Container unchanged. ***\n";
    }
}

int Container::size()const{
    return sizeC; //correct?
}

/*
bool Container::operator==(const Container &rhs,const Container &lhs){
    int equal = 0, i = 0;
    for (i = 0; i < capacityC ; i++){
        if ( rhs.elements[i] == lhs.elements[i] ){
            equal++;
        }
    }

    if (equal == sizeC){
        return true;
    }else{
        return false;
    }
}

ostream & operator<< (ostream & out, const Container & aCont){
    int i = 0;
    for (i = 0; i<sizeC; i++){
        out<< aCont.elements[i] << " " << endl;
    }
}


*/

私はヘッダファイルに他の機能を持っていません(ただの気まぐれです)。とにかく、「/* */」の最後の 2 つの関数が機能しません。ここで何が間違っているのでしょうか?

最初の関数は、2 つの配列が互いに等しいかどうかを確認することです。

4

2 に答える 2

7

関数をクラスの内部として宣言するとfriend、その関数は非メンバー関数になり、外側の名前空間で宣言されたかのようになります。だから、あなたの場合、友達の宣言operator==

class Container
{
    friend bool operator==(const Container &rhs,const Container &lhs);
};

次のように、クラスの外で宣言したかのように非メンバー関数です。

class Container
{
};

bool operator==(const Container &rhs,const Container &lhs);

フレンド関数を宣言すると、関数はクラスのプライベート メンバーにもアクセスできるため、これはまったく同じではないことに注意してください。

したがって、operator==あたかもメンバー関数であるかのように定義するのは正しくありません。

bool Container::operator==(const Container &rhs,const Container &lhs) { ... }

する必要があります

bool operator==(const Container &rhs,const Container &lhs) { ... }

オーバーロードに関してoperator<<は、 のフレンドではないため、 のプライベートメンバーにContainerアクセスできません。フレンドを作成するか、パブリック アクセサーをクラスに追加して、それらを介してプライベート メンバーにアクセスできるようにします。elementsContaineroperator<<

于 2010-04-16T06:38:15.683 に答える
0

James がすでに指摘したように、いくつかのコンパイルの問題と、いくつかの設計上の問題があります。あなたの場合、2 つのコンテナーが等しいとはどういう意味ですか? 格納されたオブジェクトのサイズと値は同じですか? 容量も?

とにかく、の簡単なリファクタリングは次のoperator==ようになります。

bool operator==( Container const & lhs, Container & rhs )
{
   if ( lhs.size() != rhs.size() ) return false;
   if ( lhs.capacity() != rhs.capacity() ) return false; // optional if same capacity is required
   for ( int i = 0; i < lhs.size(); ++i ) { // Note: only check valid objects
                                            // memory in [size,capacity) can or not be 
                                            // equal and should not affect the result
      if ( lhs[i] != rhs[i] ) return false;
   }
   return true; // all tests passed
}

あなたの実装との違いは (メンバー メソッドとして実装しようとしたという事実を無視して)、このバージョンはすぐに失敗することです。結果が判明するとすぐに、呼び出し元に返されます。サイズが異なる場合、すべての要素をチェックする必要はありません。また、コンテナに存在しない要素を比較しても意味がありません。data[size][ , )のいずれかの要素がdata[capacity]2 つの配列で一致する場合equals、結果に影響を与えるカウントに追加されます。

于 2010-04-16T07:53:47.053 に答える