views:

54

answers:

2

hello there, 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

+7  A: 

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.

James McNellis
A: 

There are some compile problem as James already pointed out, and also some design issues. In you case, what does it mean for two containers to be equal? Same size and value of the stored objects? Also capacity?

Anyway, a simple refactor of the operator== would be:

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
}

The differences from your implementation (ignoring the fact that you tried to implement it as a member method) is that this version will fail fast: As early as the result is known, it is given back to the caller. No need to check all elements if sizes differ. Also, there is no point in comparing elements that are not present in the container. If any element in [data[size], data[capacity]) coincides in the two arrays it will add to the equals count influencing your result.

David Rodríguez - dribeas