【问题标题】:friendship and operator overloading help友情和运营商重载帮助
【发布时间】:2010-04-16 06:31:09
【问题描述】:

我有以下课程

#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)。无论如何,“/* */”中的最后两个函数我无法工作,我在这里做错了什么?

第一个功能是看两个数组是否相等

【问题讨论】:

    标签: c++ arrays class containers friend


    【解决方案1】:

    当您在类中将函数声明为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&lt;&lt;重载,它不是Container的朋友,所以它无权访问Container的私有elements成员。要么让operator&lt;&lt; 成为朋友,要么向该类添加公共访问器,以便它可以通过它们访问私有成员。

    【讨论】:

      【解决方案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]) 中的任何元素在两个数组中重合,它将添加到 equals 计数中,从而影响您的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-27
        • 1970-01-01
        相关资源
        最近更新 更多