简体   繁体   中英

How can I pass a int value to my member function operator += to insert int values into a object list in c++?

I'm very new to c++ and have issues doing what seems like a simple task. I was given a main function to test and I have to create a class that correctly corresponds to the int main. Below is the class I've made so far. There's a lot more to it than what I've provided because I'm having issues starting off the basic parts of the class.

The task is to create a class that makes object lists of ints and can be manipulated with other functions that come later. For now, I am trying to set up the lists and inserting int n values.

#include<iostream>
#include<string>
#include<cmath>
#define DEFAULT 8
#define EMPTY -1
using namespace std;
// Your class specification goes here <<<<<<<<<<<<<<
class Myclass
{
  private:
   int *a = NULL;
   int end = EMPTY; 
   int size;
  public:
    Myclass(){} //default constructor
    Myclass(Myclass const &other);//copy constructor
    Myclass(Myclass &&other);//move constructor
    ~Myclass() {delete [] a;} //destructor 
    Myclass& operator=(const Myclass& other); //copy assignment
    Myclass& operator=(Myclass&& other); //move assignment
    Myclass& operator+=(const Myclass &other);
    Myclass& operator+(Myclassconst &other); // overload +
    friend ostream& operator<<(ostream &os, const Myclass& other);
};

this in my class so far and below is the definitions.

Myclass::Myclass(Myclass const &other) // copy constructor
{
  end = other.end;
  a = new int [DEFAULT];
  for (int i = 0; i <= end; i++)
   a[i] = other.a[i];
}
Myclass::Myclass(Myclass &&other) //move constructor
{
  a = new int [DEFAULT];
  for (int i = 0; i <= end; i++)
   a[i] = other.a[i];  
}
Myclass& Myclass::operator=(const Myclass& other) //overload =
{
  if (this == &other) // Check for self assignment
   return *this;
  end = other.end;
  delete [] a;
  a = new int [DEFAULT];
  for (int i = 0; i <= end; i++)
   a[i] = other.a[i];
  return *this;    
}
Myclass& Myclass::operator=(Myclass&& other)
{
  if(this != &other)
  {
    delete[] a;
    a = new int [DEFAULT];
    for (int i = 0; i <= end; i++)
    {
      a[i] = other.a[i];
    }
  }
  return *this;
}
Myclass& Myclass::operator+(Myclass const &other) //overload +
{
  Myclass answer;
  int index = 0;
  for (int i = 0; i < this->end; i++)
   answer.a[index++] = this->a[i];
  for (int i = 0; i <= other.end; i++)
   answer.a[index++] = other.a[i];
  return answer;  
}
Myclass& Myclass::operator+=(const Myclass&other)
 {
   Myclass answer;
  int index = 0;
  for (int i = 0; i < this->end; i++)
   answer.a[index++] = this->a[i];
   return answer;
 }

ostream& operator<<(ostream &os, const Myclass& other)
{
  for (int i = 0; i <= other.end; i++)
   os << other.a[i] << ", ";
  return os;  
}

My main issue is getting the += operator to work correctly as I keep getting the error about adding Myclass and an int together. My += function does not seem correct either but im not sure how to even correct it. I've commented out a2 and a3 because I'm not sure how to deal with the true or false of these objects either. Please help in any way or send me in a direction of information Id really appreciate it.

Your operator+= doesn't accept an int as input, only another Alist , so a1 += n; will not work. You need to add another operator+= for that purpose.

Also, your array management is completely wrong in your constructors and assignment operators. Your copy constructor and copy assignment operator are not allocating enough memory to copy the entire other list if it has more than DEFAULT integers. And your move constructor and move assignment operator are not actually moving anything at all.

And there are other issues with all of your operators in general. Your concatenation operators are not appending to the array correctly. And the non-compound operators have the wrong return type. See What are the basic rules and idioms for operator overloading?

Try something more like this:

Alist.h

#ifndef ALIST_H
#define ALIST_H

#include <iostream>

class Alist
{
private:
    int *a = nullptr;
    int size = 0;
    int capacity = 0;

public:
    Alist() = default; //default constructor
    Alist(int initialCap);
    Alist(Alist const &other); //copy constructor
    Alist(Alist &&other); //move constructor

    ~Alist(); //destructor

    void swap(Alist &other);

    Alist& operator=(Alist other); //copy+move assignment
    Alist& operator+=(const Alist &other);
    Alist& operator+=(int value);
    Alist operator+(const Alist &other) const; // overload +
    Alist operator+(int value) const;
    //int& operator[](int index);

    friend std::ostream& operator<<(std::ostream &os, const Alist& list);
};

void swap(Alist &a1, Alist &a2);

#endif

Alist.cpp

#include "Alist.h"
#include <utility>

#define GROWTH 8 // must be a power of 2!

Alist::Alist(int initialCap)
    : Alist()
{
    if (initialCap > 0) {
        capacity = (initialCap + (GROWTH-1)) & ~(GROWTH-1);
        a = new int [capacity];
    }
}

Alist::Alist(const Alist &other)
    : Alist(other.size)
{
    size = other.size;
    for (int i = 0; i < size; ++i) {
        a[i] = other.a[i];
    }
}

Alist::Alist(Alist &&other)
    : Alist()
{
    other.swap(*this);
}

Alist::~Alist()
{
    delete [] a;
}

void Alist::swap(Alist &other)
{
    std::swap(capacity, other.capacity);
    std::swap(size, other.size);
    std::swap(a, other.a);
}

Alist& Alist::operator=(Alist other)
{
    other.swap(*this);
    return *this;
}

Alist Alist::operator+(const Alist &other) const
{
    Alist answer(size + other.size);
    answer += *this;
    answer += other;
    return answer;
}

Alist Alist::operator+(int value) const
{
    Alist answer(size + 1);
    answer += *this;
    answer += value;
    return answer;
}

Alist& Alist::operator+=(const Alist &other)
{
    if ((size + other.size) > capacity)
    {
        Alist tmp(size + other.size);
        tmp += *this;
        tmp.swap(*this);
    }
    for (int i = 0; i < other.size; ++i) {
        a[size++] = other.a[i];
    }
    return *this;
}

Alist& Alist::operator+=(int value)
{
    if ((size + 1) > capacity)
    {
        Alist tmp(size + 1);
        tmp += *this;
        tmp.swap(*this);
    }
    a[size++] = value;
    return *this;
}

//int& Alist::operator[](int index)
//{
// return a[index];
//}

std::ostream& operator<<(std::ostream &os, const Alist& list)
{
    for (int i = 0; i < list.size; i++)
        os << list.a[i] << ", ";
    return os;
}

void swap(Alist &a1, Alist &a2)
{
    a1.swap(a2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM