sil*_*ent 0 c++ arrays containers class friend
我有以下课程
#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
Run Code Online (Sandbox Code Playgroud)
和这个源文件
#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;
}
}
*/
Run Code Online (Sandbox Code Playgroud)
我在头文件中没有其他功能(只是一个quikie).无论如何,"/**/"中的最后两个函数我无法工作,我在这里做错了什么?
第一个函数是查看两个数组是否彼此相等
当您将函数声明为friend类的内部时,该函数是非成员函数,就好像它是在封闭的命名空间中声明的一样.所以,在你的情况下,你的朋友声明operator==,
class Container
{
friend bool operator==(const Container &rhs,const Container &lhs);
};
Run Code Online (Sandbox Code Playgroud)
是一个非成员函数,就好像你已经在类之外声明它一样,如下所示:
class Container
{
};
bool operator==(const Container &rhs,const Container &lhs);
Run Code Online (Sandbox Code Playgroud)
请注意,当您声明友元函数时,该函数也可以访问该类的私有成员,因此这不完全相同.
所以,你的定义operator==好像是一个成员函数是不正确的:
bool Container::operator==(const Container &rhs,const Container &lhs) { ... }
Run Code Online (Sandbox Code Playgroud)
应该
bool operator==(const Container &rhs,const Container &lhs) { ... }
Run Code Online (Sandbox Code Playgroud)
至于你的operator<<重载,它不是朋友Container,因此它无权访问私有elements成员Container.要么成为operator<<好友,要么为该类添加公共访问者,以便它可以通过它们访问私有成员.