我有以下课程
#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)。无论如何,“ / * * /”中的最后两个函数无法正常工作,我在这里做错了什么?
第一个功能是查看两个数组是否相等
最佳答案
当您在类内部将函数声明为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
的朋友,因此它无权访问elements
的私有Container
成员。可以使operator<<
成为朋友,或将公共访问者添加到该类中,以便它可以通过它们访问私有成员。