友谊和运算符重载帮助

发布于 2024-08-29 06:41:51 字数 3023 浏览 3 评论 0 原文

我有以下类

#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)。无论如何,“/* */”中的最后两个函数我无法开始工作,我在这里做错了什么?

第一个功能是查看两个数组是否相等

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难以启齿的温柔 2024-09-05 06:41:51

当您将函数声明为类内部的友元时,该函数是非成员函数,并且就像是在封闭的命名空间中声明的一样。因此,在您的情况下,您的友元 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<< 重载,它不是 < 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 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.

一场春暖 2024-09-05 06:41:51

正如詹姆斯已经指出的那样,存在一些编译问题,还有一些设计问题。在你的例子中,两个容器相等意味着什么?存储对象的大小和值相同吗?还有容量?

无论如何,operator== 的一个简单重构将是:

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
}

与您的实现的差异(忽略您尝试将其实现为成员方法的事实)是该版本将快速失败:当结果已知时,它会返回给调用者。如果大小不同,则无需检查所有元素。此外,比较容器中不存在的元素是没有意义的。如果 [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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文