【问题标题】:How do I overload the << operator correctly in order to return an int value?如何正确重载 << 运算符以返回 int 值?
【发布时间】:2018-10-06 14:11:45
【问题描述】:

我似乎无法正确重载 &lt;&lt; 运算符。这是我到目前为止的代码,我的作业说明将在下面。如果您指出我犯的其他错误,那将是好的,但我的问题是如何正确重载我的 &lt;&lt; 运算符?

INTCOLLECTION.h:

#ifndef INTCOLLECTION_H
#define INTCOLLECTION_H

// Allocate memory in chunks of ints of this size.
const int CHUNK_SIZE = 5;

class IntCollection
{
  private:
  // The number of ints currently stored in the int
    int size;
  // the total number of elements available for storage
  // in the data array
    int capacity;
  // A pointer to the dynamically allocated data array
    int* data;
  // a private member function to allocate more memory 
  // if necessary
    void addCapacity();
  public:
  // Constructor
    IntCollection();
  // Destructor
    ~IntCollection();
  // Copy constructor:
    IntCollection(const IntCollection &c);

    void add(int value);
    int get(int index);
    int getSize();
    IntCollection& operator=(const IntCollection &c);
    bool operator==(const IntCollection &c);
    IntCollection& operator<<(int value);
};

#endif

INTCOLLECTION.cpp:

#include "IntCollection.h"
#include <iostream>
#include <cstdlib>
using namespace std;

IntCollection::IntCollection()
{
  // Initialize member data to reflect an empty 
  // IntCollection
  size = capacity = 0;
  data = NULL;
}

IntCollection::~IntCollection()
{
  delete [] data;
}

IntCollection::IntCollection(const IntCollection &c)
{
  size = c.size;
  capacity = c.capacity;
  data = c.data;

  for(int i = 0; i < c.size; i++)
  {
    data[i] = c.data[i];
  }
}

void IntCollection::addCapacity()
{
  // Create a new, bigger buffer, copy the current data to
  // it, delete the old buffer, and point our data 
  // pointer to the new buffer
  int *newData;
  data = new int[capacity];
  capacity += CHUNK_SIZE;
  newData = new int[capacity];

  for(int i = 0; i < size; i++)
  {
    newData[i] = data[i];
    delete [] data;
    data = newData;
  }
}

void IntCollection::add(int value)
{
  //first, allocate more memory if we need to
  if(size == capacity)
  {
    addCapacity();
  }
  //Now, add the data to our array and increment size
  data[size++] = value;
}

int IntCollection::get(int index)
{
  if (index < 0 || index >= size)
  {
    cout << "ERROR: get() trying to access index out of range.\n";
    exit(1);
  }
  return data[index];
}

int IntCollection::getSize()
{
  return size;
}

IntCollection& IntCollection::operator=(const IntCollection &c)
{
  size = c.size;
  capacity = c.capacity;
  data = c.data;

  return *this;
}

bool IntCollection::operator==(const IntCollection &c)
{
  if((size == c.size) && (capacity == c.capacity))
  {
    for(int m = 0; m < size; m++)
    {
      if(data[m] == c.data[m])
      {
        continue;
      }
      else
      {
        return false; 
      }
    }
  }
  return true;
}

IntCollection& IntCollection::operator<<(int value)
{
  return value; // <-- THIS IS WHERE I GET LOST!
  /* I also tried changing the parameters to
  (IntCollection &b, int value) to return b
  but my teacher wants only one parameter
  and it wasn't working that way either. */
}

说明:

对于此分配,您将向 IntCollection 类添加一个复制构造函数、一个析构函数和三个重载运算符。在下面的设计图中,黑色的成员函数代表已经实现的代码。您将实施绿色项目。您将添加到类中的每个项目都在图表下方进行了描述。

私人:

int size // 当前存储在 int 集合中的 int 数量

int capacity //数据数组中可用元素的总数

int* data // 指向动态分配的数据数组的指针

void addCapacity(); // 必要时分配更多内存的私有函数

公开:

IntCollection()

~IntCollection()

IntCollection(const IntCollection &c)

空加(整数值)

int get(int 索引)

int getSize()

IntCollection& operator=(const IntCollection &c)

bool operator==(const IntCollection &c)

IntCollection& 运算符

复制构造函数。复制构造函数应该执行参数对象的深层复制,即它应该构造一个与参数具有相同大小和容量的 IntCollection,并具有参数数据数组的完整副本。

赋值运算符 (=)。赋值运算符还应该执行参数对象的深层复制。它必须返回自身(或者更有效地返回对自身的引用)以支持同一行上的多个赋值,例如a = b = c。如果你首先实现你的赋值运算符,它可以在复制构造函数中使用,但这不是必需的。

Is 等于运算符 (==)。如果参数对象的大小与接收对象的大小相同,并且两个对象的数据数组中的值相同,则“等于”运算符应返回 true。

插入运算符 (

析构函数。函数 add() 在需要更多空间时调用 addCapacity() 来分配内存。在这个程序中没有任何地方使用 delete [] 释放内存,这意味着我们有内存泄漏!添加正确处理此问题的析构函数。

增加容量。注意 addCapacity() 是一个私有成员函数。如果您尝试从类外部调用它,即通过将下面的行添加到 main(),会发生什么?

【问题讨论】:

  • 查看我对this question 的回复。您正在处理与其他提问者相同的任务,并且您有几个与他的代码相同的错误,以及一些他的代码没有的您自己的错误。
  • 您的作业不需要 operator&lt;&lt; 返回 int 值。

标签: c++ class object operator-overloading


【解决方案1】:

你需要返回*this,即被操作的对象。返回“按引用”与返回“按值”具有相同的语法;唯一的区别是在已经提供的函数声明中添加了&amp;

试试这个:

IntCollection& IntCollection::operator<<(int value)
{
  add(value);
  return *this; 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多