Accessing array values within class, c++ - c++

I am implementing a set in C++ using an array where elements with values of 0 are not in the set and those with 1 are in the set. For example, if my set is {1,5} my array would look like: [0, 1, 0, 0, 0, 1]. I am overloading the "/" operator to show the set difference, so if x is in set A and x is not in set B then x is in A/B. I pass the second Set object to the overloaded function by reference, but I am unable to access it's member variable "arr" which stores the array.
This is the header file:
class Set
{
public:
Set();
virtual ~Set();
Set(int);
int* arr;
int len;
Set operator/(const Set&);
friend ostream& operator<<(ostream&, const Set&);
int* getArray() const;
};
And here is the .cpp file:
Set::Set(int n) {
arr = new int[n+1];
len = n+1;
}
Set Set::operator/(const Set &s){
int* arr2 = s.getArray();
for (int i = 0; i < this->len; i++) {
if (this->arr[i] == 1) {
if (arr2[i] == 0) {
this->arr[i] = 1;
}
else {
this->arr[i] = 0;
}
}
}
return *this;
}
int* Set::getArray() const { return arr; }
If I attempt to print the values of arr2 I get values that appear to be addresses. How do I access the actual values of arr2?
Edit: I have not learned the "Rule of 3" in any class I've taken so I have no understanding how this could make my code not work. I've just done some research on it, but I would appreciate a short explanation on how to implement that in this case. Also, I am not allowed to use containers, so I can't use a vector as suggested in the comments.

Related

Sorting doesn't work with templated class

I have an insertion sort function
void insertionSort(ArrayList<int> myData)
{
for (int i = 1; i < myData.getSize(); i++) {
int index = myData[i];
int j = i;
while (j > 0 && myData[j-1] > index) {
myData.swap(j - 1, j);
j--;
}
myData[j] = index;
}
}
which uses this swap function
template<class TYPE>
void ArrayList<TYPE>::swap(int from, int to) throw(std::out_of_range)
{
int temp = 0;
temp = this->items[from];
this->items[from] = this->items[to];
this->items[to] = temp;
swapNum++;
}
This is how my private methods look like
TYPE * items;
int currentLength;
static int swapNum;
I have an overloaded [] operator and a getSize() function that I think I wrote well and not contributing to my problem. Now if I do this in my main.cpp
ArrayList<int>m_Data(1);
and append say 4,2,9,1 on the m_Data and call
insertionSort(m_Data);
I get two errors
1. Error C2440 '=': cannot convert from 'std::string' to 'int'
on the swap function and
2. The insertion sort doesn't work
First problem: it should be something like TYPE temp = this->items[from]. After repairing it (I used STL swap) function works. Well, it works on STL vector and swap. If you still do have problem, then your array structure is probably invalid.
EDIT: In function 'insertionSort' shouldn't you have template (as in swap function)?

C++ Making a 2D boolean matrix

I am making a program where I have 2 vectors (clientvec and productslist) and I need to create a 2D boolean matrix where the columns is the size of productslist vector and the lines is the size of clientvec vector, but it gives me this error:
"expression must have a constant value"
Here is the code I used:
unsigned int lines = clientvec.size();
unsigned int columns = productslist.size();
bool matrixPublicity[lines][columns] = {false};
Pls help me..
Edit: I am new at c++ so assume I know nothing xD
Edit2: I already know for the answers that I cannot initialize an array with non constant values, now the question is how can I put them after initialize...
The error message is clear: :expression must have a constant value"
It means the array dimension cannot be of variable type. Only enums or pre-processor defined constants are valid.
See for more info:
Why can't I initialize a variable-sized array?
Edit: Since you mentioned you are new to C++, here is a piece of code that might help you:
#include <iostream>
#include <vector>
#include <bitset>
int main()
{
unsigned int lines = 10;
const unsigned int columns = 5;
std::vector<std::bitset<columns>> matrixPublicity;
matrixPublicity.resize(lines);
for(int i=0; i < lines; i++)
{
for(int j=0; j < columns; j++)
std::cout << matrixPublicity[i][j] <<' ';
std::cout<<'\n';
}
}
note that in this case, columns must be constant.
Edit 2: And if the size of lines are not the same, then you must stick to vector types:
typedef std::vector<bool> matrixLine;
std::vector<matrixLine> matrixPublicity;
now you can use resize method for the i-th line of the matrix, e.g.
matrixPublicity[1].resize(number_of_columns_in_line_2);
What you are trying to do would be the same as this:
std::vector<unsigned int> v1 { 1, 2, 3, 4, 5 };
std::vector<unsigned int> v2 { 6, 7, 8, 9 };
bool mat[v1.size()][v2.size()] = false;
This is how the compiler will interpret it without the temporaries and this is invalid. When you declare an array of any type its size has to be known at compile time.
bool mat[2][3] = false; // still invalid
bool mat[2][3] = { false }; // Okay
const int x = 5;
const int y = 7;
bool mat[x][y] = false; // invalid
bool mat[x][y] = { false }; // okay
// Even this is invalid
std::vector<int> v1{ 1, 2, 3 };
std::vector<int> v2{ 4, 5, 6, 7 };
const std::size_t x1 = v1.size();
const std::size_t y1 = v2.size();
bool mat2[x1][y1] = { false }; // Still won't compile.
Value to declare an array must be a constant expression.
Instead of making an array as you have tried to do, you could make a class template that will construct a matrix like object for you. Here is what I have come up with, now the overall design or pattern of this template will fit your condition but the actual implementation to generate the internal matrix will depend on your data and what you intend.
#include <vector>
#include <iostream>
#include <conio.h>
template <class T, class U>
class Matrix {
private:
std::vector<T> m_lines;
std::vector<T> m_cols;
std::vector<U> m_mat;
std::size_t m_size;
std::size_t m_lineCount;
std::size_t m_colsCount;
public:
Matrix() {};
Matrix( const std::vector<T>& lines, const std::vector<T>& cols ) :
m_lines(lines),
m_cols(cols),
m_lineCount( lines.size() ),
m_colsCount( cols.size() )
{
addVectors( lines, cols );
}
void addVectors( const std::vector<T>& v1, const std::vector<T>& v2 ) {
m_lines = v1;
m_cols = v2;
m_lineCount = m_lines.size();
m_colsCount = m_cols.size();
for ( unsigned int i = 0; i < m_lineCount; ++i ) {
for ( unsigned int j = 0; j < m_colsCount); j++ ) {
// This will depend on your implementation and how you
// construct this matrix based off of your existing containers
m_mat.push_back(m_lines[i] & m_cols[j]);
}
}
m_size = m_mat.size();
}
std::size_t size() const { return m_size; }
std::size_t sizeRows() const { return m_lineCount; }
std::size_t sizelColumns() const { return m_colsCount; }
std::vector<U>& getMatrix() const { return m_mat; }
std::vector<T>& getLines() const { return m_lines; }
std::vector<T>& getColumns() const { return m_columns; }
bool operator[]( std::size_t idx ) { return m_mat[idx]; }
const bool& operator[]( std::size_t idx ) const { return m_mat[idx]; }
};
int main() {
std::vector<unsigned> v1{ 1, 0, 1, 1, 0 };
std::vector<unsigned> v2{ 0, 1, 1, 1, 0 };
Matrix<unsigned, bool> mat1( v1, v2 );
int line = 0;
for ( unsigned u = 0; u < mat1.size(); ++u ) {
line++;
std::cout << mat1[u] << " ";
if ( line == mat1.sizeRows() ) {
std::cout << "\n";
line = 0;
}
}
std::cout << "\nPress any key to quit.\n" << std::endl;
_getch();
return 0;
}
Output
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0
With this template class you can create a matrix of any type U by passing in two vectors for type T. Now how you construct the matrix will be implementation dependent. But this class is reusable for different types.
You could have two vectors of type doubles, and construct a matrix of unsigned chars, or you could have two vectors of user defined class or struct types and generate a matrix of unsigned values. This may help you out in many situations.
Note: - This does generate a compiler warning, no errors though and it prints and displays properly, but the compiler warning generated by MSVS 2015 is warning C4800: unsigned int: forcing value to bool true or false (performance warning)
This is generated for I am doing a bit wise & operation on to unsigned values; but that is why I set my initial vectors to be passed to this class template's constructor to have all 1s & 0s as this is meant for demonstration only.
EDIT - I made an edit to the class because I noticed I had a default constructor and had no way to add vectors to it, so I added an extra member variable, and an addVectors function, and moved the implementation from the defined constructor to the new function and just ended up calling that function in the defined constructor.
Creating an array isn't that difficult :)
A matrix (2D/3D/...-array) is unfortunately a little bit different if you want to do it your way!
But first of all you should know about the stack and the heap!
Lets have a look at these 2:
Stack:
A stack variable/array/matrix/... is only valid between the nearest 2 -> {} <- which you normally call a "codeblock". The size of it was defined during the "compile time" (the time where the compiler translates your code into the machine language). That means the size of your array needs to be set.
Example:
#include <iostream>
#define MACRO 128
int arraySize(int size){
std::cin >> size;
return size;
}
int main() {
//this is valid
int intArray[128] = {}; //the size(here: 128) needs to be a number like
//or a macro like 'MACRO' which is
//compile-time-only as well
//this is valid
int intArray2[MACRO] = {};
//this is not valid!
int intArray[size()] = {};
return 0;
}
Heap:
A heap variable/array/matrix/... is valid until you delete it. That also means that a heap var is created during the run-time(from starting your program until you close/stop it)! This is allows you to define it's size.
Example:
#include <iostream>
#define MACRO 128
int arraySize(int size){
return size;
}
int main() {
//this is valid
int intArray[128] = {}; //the size(here: 128) needs to be a number like
//or a macro like 'MACRO' whic is
//compile-time-only as well
//this is valid
int intArray2[MACRO] = {};
//creating an array with a non-static size
//works like this:
//int can also be a 'bool'
int* intArray = new int[arraySize()];
// ^ the star means you are pointing to
//an adress inside of your memory which has
//the size of an int (per element)
//That's why they are called "pointers"!
//Right now it points to the beginning of the
//array.
// ^ the keyword "new" says that
//you are allocating memory on the heap.
// ^
//then you have to say which kind of array
//it is which is the same you gave the pointer
// ^
//now you give it the size of that array
//this time it can be return value or the size
//of a variable
//as I mentioned...you have to delete this array on your own
//if you dont do that your program will crash
//maybe not after starting but it will!
//SO NEVER NEVER NEVER... forget about it
delete intArray[];
//^ write delete
// ^
//then the name of your array
// ^
//at the end of it write these 2 brackets
//thex say you wanna remove the whole array!
//why? because you can also create/delete
//heap variables not only arrays.
return 0;
}
Creating a matrix on the heap is unfortunately not that easy.
But it is essential to know how a 1D-array works before going to further dimensions! That's why I did this tutorial!
Klick here to see how to create a matrix on the heap
Klick here to learn more about the heap
Klick here to choose the best result of this theme
I hope I could help you :)!

Cplusplus std::set of bidimensional array

I'm new to C++ and I need to use Set from the STL but I'm struggling with the concept.
I have an array like this int my_data[3]
I want to create (don't know if this is possible) a set with space for 3 ints and that the key for the set would be the int that is stored on the first column.
I also want to make a custom sort.
This is what I've tried but with no success.
struct sort_set {
bool operator() (const int& a, const int& b) const {
int* arr1 = (int*) a;
int* arr2 = (int*) b;
int diff = arr2[1] - arr1[1];
if (diff) {
return true;
} else if (diff == 0) {
int diff2 = arr2[2] - arr1[2];
if (diff2) {
return false;
}
}
return arr1[0] < arr2[0];
}
};
set<int[3],sort_set> data;
Can someone point me in the right direction?
You cannot have arrays as elements of containers. They're not assignable nor copyable.
Use std::array<int, 3> if you have C++11 avaliable, or define a custom class otherwise.

union of 2 sets; return simple object - C++

What is the best way to implement the following? I am trying to find the union of 2 sets. I am creating 2 objects (one called set1 and one called set2). I aim to create a 3rd object that is a UNION of the two without having to use a copy constructor. Using dynmanic memory allocation and pointers and/or references is a must. Thanks to anyone to solves this dilemma and any pointers (no pun intended) would help.
Thanks coders.
THE HEADER file
#ifndef INTEGERSET_H_
#define INTEGERSET_H_
class IntegerSet
{
private:
int * set;
int set_size;
public:
IntegerSet(int size); //default constructor
~IntegerSet(); //destructor
IntegerSet * unionOfSets(const IntegerSet & set2);
void insertElement(int k) const;
void printSet(int size) const;
};
#endif
THE MAIN file
#include <iostream>
#include "integerset.h"
using std::cout;
using std::endl;
int main()
{
IntegerSet set1(11);
//testing below
set1.insertElement(3);
set1.insertElement(4);
set1.insertElement(6);
set1.insertElement(10);
set1.printSet(11);
cout << endl;
IntegerSet set2(8);
set2.insertElement(3);
set2.insertElement(6);
set2.insertElement(7);
set2.printSet(11);
cout << endl;
IntegerSet * obj3 = new IntegerSet(11);
obj3 = set1.unionOfSets(set2);
obj3->printSet(11);
// system("pause");
return 0;
}
THE IMPLEMENTATION FILE
#include "integerset.h"
#include <iostream>
IntegerSet::IntegerSet(int size)
{
set = new int[size];
set_size = size;
for (int i = 0; i < size; i++)
set[i] = 0;
}
IntegerSet::~IntegerSet()
{
delete [] set;
}
void IntegerSet::insertElement(int k) const
{
(*this).set[k] = 1;
}
void IntegerSet::printSet(int size) const
{
int temp = 0;
for (int i = 0; i < size; i++)
{
if (set[i] == 1)
{
std::cout << i << " ";
temp++;
}
}
if (temp == 0)
std::cout << "----";
}
IntegerSet * IntegerSet::unionOfSets(const IntegerSet & set2) //make this return the union of 2 sets; THIS and the passed ARG reference; return address
{
return this;
}
Random morning rant
What you try to create is more a std::bitset than a std::set. A set is usually "a collection of well defined distinct objects" (Cantor's definition is a little bit more complex, but lets stick to this). As such, a set could contain several pairwise unrelated objects.
Now, after this has been said, have a look at std::bitset. Note that its size is fixed by the template parameter N. std::bitset::set is almost equivalent to your IntegerSet::insertElement, except that it throws std::out_of_range. This I recommend you to check your index for valid position:
void IntegerSet::insertElement(int k) const
{
if( k < 0 || k >= set_size)
throw std::out_of_range;
else
this->set[k] = 1;
}
However, std::bitset doesn't support unions, so it's time to address your meain conern.
IntegerSet::unionofSets
Have a look at those lines.
IntegerSet * obj3 = new IntegerSet(11);
obj3 = set1.unionOfSets(set2);
The first line initializes obj3 with a pointer which contains the memory for a newly created IntegerSet with an internal set of size 11. And in the next line, your throw that pointer away. So you're throwing away resources and create a memory leak.
If you were to create a new IntegerSet your solution would be quite simple:
IntegerSet IntegerSet::unionOfSets(const IntegerSet & set2) const
{
IntegerSet tmp (std::max(set2.set_size, this->set_size));
for(int i = 0; i < set_size; ++i)
tmp.set[i] = this->set[i];
for(int i = 0; i < set2.set_size; ++i)
tmp.set[i] |= set2.set[i];
return tmp;
}
But your implementation changes the object it has been called from, so it isn't const and a little bit different:
IntegerSet * IntegerSet::unionOfSets(const IntegerSet & set2) // not const!
{
if(set2.set_size > set_size){
// the resulting set is bigger, we need new memory
int * newset = new int[set2.set_size];
// copy old values
for(int i = 0; i < this->set_size; ++i)
newset[i] = this->set[i];
// replace old size
this->set_size = set2.set_size;
delete[] this->set; // remove old data
this->set = newset; // replace pointer
}
for(int i = 0; i < set2.set_size; ++i)
this->set[i] |= set2.set[i];
return this;
}
This should be sufficient. Keep in mind that you must not use new IntegerSet in order to create a union:
IntegerSet * obj3 = new IntegerSet(11); // new memory, lets say obj3 = 0x500a
obj3 = set1.unionOfSets(set2); // new memory gone forever
if(obj3 == &set1)
std::cout << "obj3 is a pointer to set1, changes to obj3 will affect set1" << std::endl;
If you don't want to create this behaviour use the first version with the temporary.
Also, please check whether std::set<int> is sufficient for you, as you can use std::set_union from <algorithm>.
EDIT
Provide a unionOfSets member function that creates a third IntegerSet that is the union of two existing IntegerSet instances (so the third set created by this function contains all the members in the two sets used to create it – so if one or both of the sets the union is performed on has an element in it, the third set will have that element)
In this case forget about IntegerSet * IntegerSet::unionOfSets(const IntegerSet&) and use IntegerSet IntegerSet::unionOfSets(const IntegerSet&) const (the first variant with a returned object instead of a returned pointer).
EDIT2
As you didn't follow the rule of three, the memory in the returned IntegerSet will be invalid. You would either have to implement a copy constructor/assignment operator in order to fix this, or provide a new object with dynamic storage duration (new). For this you would just have to adjust the method a little bit:
IntegerSet * IntegerSet::unionOfSets(const IntegerSet & set2) const
{
IntegerSet * tmp = new IntegerSet( set2.set_size > this->set_size ? set2.set_size : this->set_size);
for(int i = 0; i < set_size; ++i)
tmp->set[i] = this->set[i];
for(int i = 0; i < set2.set_size; ++i)
tmp->set[i] |= set2.set[i];
return tmp;
}
Using standard facilities...
std::vector is better than a hand-rolled array
std::sort and std::unique are goodness
Therefore:
std::vector<int> set1;
set1.push_back(1); // ... and others
std::sort(set1.begin(), set1.end()); // sorts
std::unique(set1.begin(), set1.end()); // removes duplicates
// same with set2
std::vector<int> set3(set1);
set3.insert(set3.end(), set2.begin(), set2.end());
std::sort(set1.begin(), set1.end()); // sorts
std::unique(set1.begin(), set1.end()); // removes duplicates

A bunch of unclear things with the destructor in C++

I've written some very simple code in C++ to do some simple manipulations of vectors. This is the content of the file vector.h:
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
class Vector {
int *coordinates;
int *size;
public:
Vector(int vector_size);
Vector(int*,int);
~Vector();
void print(void);
Vector operator +(Vector);
};
#endif
and this is the implementation (file: vector.cpp):
#include "vector.h"
#include <iostream>
using namespace std;
Vector::Vector(int vector_size) {
coordinates = new int[vector_size];
size = new int;
*size = vector_size;
}
Vector::Vector(int* vector_coordinates, int vector_size){
coordinates = vector_coordinates;
size = new int;
*size = vector_size;
}
void Vector::print(void){
cout << "[";
for (unsigned short int index =0; index<*size; index++){
cout << coordinates[index];
if (index < *size-1){cout << ", ";};
}
cout << "]\n";
}
Vector Vector::operator+ (Vector other) {
Vector temp(*(other.size));
if ((*temp.size)!=(*(this->size))){
throw 100;
}
int* temp_c = new int[*(other.size)];
int* other_c = other.coordinates;
for (unsigned short int index =0; index<*size; index++){
temp_c[index] = coordinates[index] + other_c[index];
}
temp.coordinates = temp_c;
return (temp);
}
Vector::~Vector(){
delete[] coordinates;
delete size;
}
From my main.cpp, I do the following:
#include <iostream>
using namespace std;
#include "vector/vector.h"
const int size = 3;
int main() {
int *xxx = new int[size];
xxx[0]=4; xxx[1]=5; xxx[2]=-6;
Vector v(xxx,size);// v = [4, 5, -6]
Vector w(size);// w is a vector of size 3
w = v+v; // w should be w=[8,10,-12]
w.print();
return 0;
}
The result is then:
[148836464, 5, -6, 17, 148836384, 0, 0, 17, 0, 0, 0, 17, 3, 0, 0, 17, 0, 0, 0, 17, 148836480, 0, 0, 17, 0, 10, -12, 135025, 0, 0, 0, 0, 0, 0, , 0, 0,Segmentation fault
If I remove the two lines from the destructor:
delete[] coordinates;
delete size;
everything works as expected and the program outputs:
[8, 10, -12]
I would appreciate any explanations...
Update 1: I changed my operator+ method to the following, but the problem was not resolved:
Vector Vector::operator+(Vector other) {
int size_of_other = *(other.size);
int size_of_me = *(this->size);
if (size_of_other != size_of_me) {
throw 100;
}
int* temp_c = new int[size_of_me];
int* other_c = other.coordinates;
for (unsigned short int index = 0; index < size_of_me; index++) {
temp_c[index] = coordinates[index] + other_c[index];
}
Vector temp(temp_c,size_of_me);
return (temp);
}
Update 2: I noticed that using the operator:
Vector Vector::operator+(Vector other);
I wouldn't get the desired result. The modification that made it work was:
const Vector& Vector::operator+(const Vector& other) {
Vector temp(other.size);
for (unsigned short int index = 0; index < size; index++) {
cout << "("<< index <<") "<<coordinates[index] << "+"
<<other.coordinates[index] << ", "<< endl;
temp.coordinates[index] = coordinates[index] + other.coordinates[index];
}
return (temp);
}
Update 3: After update #2, I was getting a warning from the compiler that I return the local 'temp'. I changed my code to the following which completely resolved all problems and works fine (I return a copy of temp):
const Vector Vector::operator+(const Vector& other) const{
Vector temp(other.size);
for (unsigned short int index = 0; index < size; index++) {
temp.coordinates[index] = coordinates[index] + other.coordinates[index];
}
return *(new Vector(temp));
}
Your Vector::operator+ has at least one bug:
int* temp_c = new int;
...
temp_c[index] =
You are indexing temp_c when it was allocated with only a single integer. So your loop is stomping on some other memory, causing undefined behaviour.
You will also need to define a copy constructor so that you can properly use your Vector objects. The compiler generates a default copy constructor, but the default one is generally not suitable for objects that contain pointers.
This line:
temp.coordinates = temp_c;
causes a memory leak, because it overwrites the previously allocated temp.coordinates vector.
Update 3: Your code
return *(new Vector(temp));
while it appears to work, is still a memory leak. You are allocating a new Vector, then the compiler calls the copy constructor to copy that into the return value of your function. Nobody ever deletes the Vector object you just created, so there is a memory leak.
The solution is to write a copy constructor, instead of relying on the compiler-generated default copy constructor. All the other answers to your question have said the same thing. It is required that you do this for a correct program.
Your class needs a copy constructor and copy assignment operator to work correctly. A big hint that they are needed is that the destructor is not {}. See the "Rule of Three".
To get a bit better and more modern, you could also consider a move constructor and move assignment operator.
Try the code below which:
Implements a default constructor. This garauntees that however your object is constructed, your internal variables are going to be pointing at something on the heap or at NULL so any delete [] calls aren't going to die horribly.
Implements a copy constructor. Default copy constructors don't copy memory on the heap so that was going to be a serious problem for you.
Implements an assignment operator. Again this avoids shallow copies.
Removes size as a pointer; On most systems, pointers are the same size as integers so making size a pointer just makes things unnecessarily complicated.
Fixes the addition constructor by avoiding intermediate allocations. You had a temporary local variable there so make use of it instead of allocating several extra intermediate objects.
...take a look:
// VectorImplementation.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
class Vector {
int *coordinates;
int size;
public:
Vector();
Vector(int vector_size);
Vector(int*,int);
Vector(const Vector& v);
~Vector();
Vector operator +(Vector);
Vector& operator =(const Vector & other);
void print(void);
};
Vector::Vector() {
coordinates = NULL;
size = NULL;
}
Vector::Vector(int vector_size) {
coordinates = new int[vector_size];
size = vector_size;
}
Vector::Vector(int* vector_coordinates, int vector_size){
coordinates = vector_coordinates;
size = vector_size;
}
Vector::Vector(const Vector& v) {
size = v.size;
coordinates = new int[size];
memcpy(coordinates,v.coordinates, sizeof(int)*size);
}
void Vector::print(void){
cout << "[";
for (unsigned short int index =0; index<size; index++){
cout << coordinates[index];
if (index < size-1){cout << ", ";};
}
cout << "]\n";
}
Vector Vector::operator+ (Vector other) {
Vector temp(other.size);
for (unsigned short int index =0; index<size; index++){
temp.coordinates[index] = coordinates[index] + other.coordinates[index];
}
return (temp);
}
Vector & Vector::operator= (const Vector & other)
{
if (this != &other) // protect against invalid self-assignment
{
// 1: allocate new memory and copy the elements
int * tmp_coordinates = new int[other.size];
memcpy(tmp_coordinates, other.coordinates, sizeof(int)*other.size);
// 2: deallocate old memory
delete [] coordinates;
// 3: assign the new memory to the object
coordinates = tmp_coordinates;
size = other.size;
}
// by convention, always return *this
return *this;
}
Vector::~Vector(){
printf("Destructing %p\n", this);
delete[] coordinates;
}
const int size = 3;
int _tmain(int argc, _TCHAR* argv[])
{
int *xxx = new int[size];
xxx[0]=4;
xxx[1]=5;
xxx[2]=-6;
Vector v(xxx,size);// v = [4, 5, -6]
Vector w(size);// w is a vector of size 3
w = v+v; // w should be w=[8,10,-12]
w.print();
return 0;
}
Doing that is a bad idea:
Vector::Vector(int* vector_coordinates, int vector_size){
coordinates = vector_coordinates;
size = new int;
*size = vector_size;
}
you assign coordinates pointer to data that you did not allocate, and then try to delete it in the destructor.
But the real reason that you get segfault is that you use the default copy constructor, and the temporary copy of v deletes the vector when it dies. You have to implement copy constructor and ensure deep copy or reference counting.
Try something like this:
Vector::Vector(const Vector& other){
size = new int(*other.size);
coordinates = new int[size];
memcpy(coordinates, other.coordinates, sizeof(int)*(*size));
}
Also, your operator+ would be much more efficient if you take const reference as an argument:
Vector Vector::operator+ (const Vector& other)
Consider the line
w = v+v; // w should be w=[8,10,-12]
a temporary object is constructed for the result of v+v, then assigned to w and destroyed.
Since you don't have and assignment operator a shallow copy is performed by the default implementation and you are working with deallocated memory.
The simple way to fix this issue is to implement a copy constructor/ assignment operator and destructor when you are allocating memory for members.