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 with a default cap of 8 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 Alist
{
private:
int *a = NULL;
int end = EMPTY; // requires -std=c++11 to compile
int size;
public:
Alist(){a = new int [DEFAULT];} //default constructor
Alist(Alist const &other);//copy constructor
Alist(Alist &&other);//move constructor
~Alist() {delete [] a;} //destructor
Alist& operator=(const Alist& other); //copy assignment
Alist& operator=(Alist&& other); //move assignment
Alist& operator+=(const Alist &other);
Alist& operator+(Alist const &other); // overload +
friend ostream& operator<<(ostream &os, const Alist& other);
};
this in my class so far and below is the definitions.
Alist::Alist(Alist const &other) // copy constructor
{
end = other.end;
a = new int [DEFAULT];
for (int i = 0; i <= end; i++)
a[i] = other.a[i];
}
Alist::Alist(Alist &&other) //move constructor
{
for (int i = 0; i <= end; i++)
a[i] = other.a[i];
*this = move(other);
}
Alist& Alist::operator=(const Alist& 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;
}
Alist& Alist::operator=(Alist&& other)
{
if(this != &other)
{
delete[] a;
a = new int [DEFAULT];
for (int i = 0; i <= end; i++)
{
a[i] = other.a[i];
}
}
return *this;
}
Alist& Alist::operator+(Alist const &other) //overload +
{
Alist 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;
}
Alist& Alist::operator+=(const Alist &other)
{
Alist answer;
int index = 0;
for (int i = 0; i < this->end; i++)
answer.a[index++] = this->a[i];
return answer;
}
//Alist& Alist::operator[](Alist &other)
//{
// return a[];
//}
ostream& operator<<(ostream &os, const Alist& other)
{
for (int i = 0; i <= other.end; i++)
os << other.a[i] << ", ";
return os;
}
I need to get my code to work with this first part of the main function
int main()
{// Test basic constructors and += overloading
Alist a1, /*a2(true), a3(72, false),*/ a4, a6, a7, a8;
const int nums_count = 100;
int n;
for (int i = 0; i < nums_count; i++)
{
n = rand() % 1000;
a1 += n;
//a2 += n;
//a3 += n;
}
return 0;
}
My main issue is getting the += operator to work correctly as I keep getting the error about adding Alist 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.
Aucun commentaire:
Enregistrer un commentaire