友谊和运算符重载帮助
我有以下类
#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)。无论如何,“/* */”中的最后两个函数我无法开始工作,我在这里做错了什么?
第一个功能是查看两个数组是否相等
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将函数声明为类内部的
友元
时,该函数是非成员函数,并且就像是在封闭的命名空间中声明的一样。因此,在您的情况下,您的友元operator==
声明是一个非成员函数,就好像您已在类外部声明它一样,如下所示:
请注意,当您声明友元函数时,该函数也可以访问该类的私有成员,因此这并不完全相同。
因此,您将
operator==
定义为成员函数是不正确的:应该是
至于您的
operator<<
重载,它不是 < code>Container,因此它无权访问Container
的私有elements
成员。要么使operator<<
成为友元,要么向类添加公共访问器,以便它可以通过它们访问私有成员。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 friendoperator==
,is a non-member function as if you had declared it outside of the class, like so:
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:should be
As for your
operator<<
overload, it is not a friend ofContainer
, so it does not have access to the privateelements
member ofContainer
. Either makeoperator<<
a friend or add public accessors to the class such that it can access the private members through them.正如詹姆斯已经指出的那样,存在一些编译问题,还有一些设计问题。在你的例子中,两个容器相等意味着什么?存储对象的大小和值相同吗?还有容量?
无论如何,
operator==
的一个简单重构将是:与您的实现的差异(忽略您尝试将其实现为成员方法的事实)是该版本将快速失败:当结果已知时,它会返回给调用者。如果大小不同,则无需检查所有元素。此外,比较容器中不存在的元素是没有意义的。如果 [
data[size]
,data[capacity]
) 中的任何元素在两个数组中重合,它将添加到影响您的equals
计数中结果。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: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 theequals
count influencing your result.