Search code examples
c++arraysclasscontainersfriend

friendship and operator overloading help


I have the following class

#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

and this source file

#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;
    }
}


*/

I dont have the other functions in the header file (just a quikie). Anyways, the last two functions in "/* */" I cant get to work, what am I doing wrong here?

the first function is to see whether the two arrays are equal to one another


Solution

  • When you declare a function as a friend inside of a class, the function is a non-member function and is as if it was declared in the enclosing namespace. So, in your case, your declaration of a friend operator==,

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

    is a non-member function as if you had declared it outside of the class, like so:

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

    Note that when you declare a friend function, the function has access to the private members of the class as well, so this isn't exactly the same.

    So, your definition of operator== as if it were a member function is incorrect:

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

    should be

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

    As for your operator<< overload, it is not a friend of Container, so it does not have access to the private elements member of Container. Either make operator<< a friend or add public accessors to the class such that it can access the private members through them.