2010-04-16 36 views
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; 
    } 
} 


*/ 

我沒有在頭文件(只是一個quikie)的其他功能。無論如何,「/ * * /」中的最後兩個函數無法工作,我在這裏做錯了什麼?

所述第一功能是將看到兩個數組是否彼此相等

回答

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朋友,因此它無法訪問Container的私人elements成員。或者讓operator<<成爲朋友,或者向該班級添加公共訪問者,以便可以通過他們訪問私人成員。

0

詹姆斯已經指出了一些編譯問題,還有一些設計問題。在你的情況下,兩個容器相等意味着什麼?存儲對象的大小和值相同嗎?還有容量?

反正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])中的任何元素在兩個陣列中一致,則會添加到影響結果的equals計數。