2010-04-16 40 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计数。