Over loading + operator to add 2 arrays and copy equal numbers once

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef DARRAY_H
#define DARRAY_H

#include <iostream>
#include <cassert>
#include <string>
using namespace std;

template<class T>
class DArray
{
	T* list;
	int size;

public:
	DArray();
	DArray(const DArray<T>&);
	~DArray();
	const DArray<T>& operator=(const DArray&);

	int length() const;

	void push_front(const T);
	void push_back(const T);
	void erase(const int);
	T pop_front();
	T pop_back();


	DArray<T> operator+(const DArray&) ;
	DArray<T> operator-(const DArray&) const;
	DArray<T> operator%(const DArray&) const;
	T& operator[](int);
	const T& operator[](int) const;

	friend ostream& operator<<(ostream& os, const DArray<T>& _d)
	{
		if ( _d.size == 0 )
		{
			cout<<"()";
			return os;
		}

		cout<<"(";
		for(int i = 0; i < _d.size-1; i++)
			cout<<_d.list[i]<<", ";
		cout<<_d.list[_d.size-1]<<")";

		return os;
	}
};
template<class T>
DArray<T>::DArray()
{
    list =0;
    size =0;
}
template<class T>
DArray<T>::DArray(const DArray& c)
{
    size = c.size;
    list = new T [size];
    for(int i=0;i<size;i++)
    list[i]=c.list[i];
}
template<class T>
DArray<T>::~DArray()
{
    delete[] list;
    list =0;
}
template<class T>
const DArray<T>& DArray<T>:: operator=(const DArray& c)
{
    if(this == &c)
    return *this;
    if(list!=0)
    {
        delete []list;
        list =0;
    }
     size = c.size;
    list = new T [size];
    for(int i=0;i<size;i++)
    list[i]=c.list[i];

    return *this;
}
template<class T>
int DArray<T>::length()const
{
    return size;

}
template<class T>
void DArray<T>::push_front(const T v)
{
    DArray<T> copy =*this;
    delete[] list;
    list = new T[size+1];
    list[0] = v;

    for(int i=0;i<size;i++)
    list[i+1]=copy.list[i];
    size++;

}
template<class T>
void DArray<T>::push_back(const T v)
{
    DArray<T> copy =*this;
    delete[] list;
    list = new T[size+1];
    list[size] = v;

    for(int i=0;i<size;i++)
    list[i]=copy.list[i];
    size++;


}
/*template<class T> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Havnt written this so far
void DArray<T>::erase(const int i)
{

}
*/
template<class T>
DArray<T> DArray<T>::operator+(const DArray& _d)
{
    DArray<T> copy =*this;
    delete[] list;
    list = new T[size+1];
    int nSize =0;

    for(int i=0;i<size;i++)
     {if (_d.list[i]!=copy.list)
            nSize++;
            else
            nSize++;
    }
    cout<<nSize;


}





#endif 

main Issue im getting is converting pointer to integer since my main is written as :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "DArray.h"

int main()
{
	DArray<int> odd, even;

	for(int i = 0; i < 5; i++)
	{
		odd.push_back(2*i+1);
		even.push_front(2*i);
	}

	cout<<"Odd #s array: "<<odd<<endl;
	cout<<"Even #s array: "<<even<<endl;

	DArray<int> sum = odd + even;

	cout<<"Sum #s array: "<<sum<<endl;

return 0;
}


what am I doing wrong ?
Last edited on
If you use code tags, one may take a look at your post.

http://cplusplus.com/articles/z13hAqkS/

Also make sure you indent your code properly (note how indentation was lost when you pasted outside code tags; re-copy).
NICE :D thx for letting me know how to do that
Ok, your operator+ doesn't return anything, and you are modifying the lhs operand, which is not necessary.

Your operator+ logic should be:

1. Create a new DArray object.
2. Resize the new DArray object's internal list array so it can fit all elements from both arrays (the this and the right-hand side operand).
3. Do a FOR loop that copies the elements in the first array into the new array.
4. Do a FOR loop that copes the elements in the second array into the new array.
5. Return the new array.

Give it a try.
Thx for the feedback ^^ made me happy and feel less stressed
but let me go with you thru what I wrote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
DArray<T> DArray<T>::operator+(const DArray& _d)
{
    DArray<T> copy =*this;
	DArray<T> _s;
    delete[] list;
    list = new T[size+1];
    int nSize =0;


    for(int i=0;i<size;i++) //here Ill get the new size needed for the new array
     {if (_d.list[i]!=copy.list[i])
            nSize++;
            else
            nSize++;
    }
    for(int i=0;i<nSize;i++) //after the previous step ill be copying the values of my original arrays in one array
	{
		if (_d.list[i]!=copy.list[i])
		{
			_s.list[i]=_d.list[i];
			_s.list[i+1]=copy.list[i];
		}
		else
			_s.list[i]=*_d.list;

		return *this; //I believe this is how its supposed to be returned, the program run perfectly, but the output I require aint availble 

	}
}


thx again for the tips :)
Incorrect. Line 5 for instance deletes the inner list of the current object (the left-hand side operand of the binary + operator). This is NOT necessary. You must follow the logic I gave you exactly. No more, no less.

Also note that I told you to return the new array (step 5), not *this. *this is the "old" array, or one of the old arrays anyway.

My advise: Start the operator+ code from scratch. Delete everything inside it and start over following my logic exactly.
Thx again,
I got it right this time with the following :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<class T>
DArray<T> DArray<T>::operator+(const DArray &_d)
{
DArray<T> result = *this;

for(int i = 0; i < _d.length(); i++)
{
   bool add = true;

  for(int j = 0; j < size; j++)
 {
    if ( _d.list[i] == list[j] )
          add = false;
  }

  if ( add == true )
     result.push_back(_d.list[i]);
}

return result;

}


But if im to implement the "-" operator shouldnt it be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<class T>
DArray<T> DArray<T>::operator-(const DArray &_d)
{
DArray<T> result = *this;

for(int i = 0; i < _d.length(); i++)
{
	bool subtract = true;

  for(int j = 0; j < size; j++)
 {
	  /*I tried doing this in a couple of ways such that if list[i]==_d.list[i]
	  , then delete the ith element , but that wasnt possible;
	  else keep the element.
	*/
  
  }
}

return result;

}

Ok, yes, better. Note: It is considered bad practice to code if() statements like you did with the boolean variable "add". The result of operator== is of type boolean, so there really is no need to apply this operator over a boolean variable. In line 16, you should instead write if (add). If you were to ask if add were false, then it would be if (!add). The same is true for integers and pointers:

1
2
3
4
5
6
7
8
9
10
MyType *myPointer = NULL;
//Don't do this:
if (myPointer == NULL) ;
//Instead do this:
if (!myPointer) ;
int myVal = 0;
//Don't do this:
if (myVal != 0) ;
//Instead do this:
if (myVal) ;


As for your operator-(), you need to explain why you were unable to do what you wanted to do, exactly.
My advice is not to overload operator+ unless it preserves its original semantics. This usage seems to be less intuitive for users, so I would use a named function that would be more descriptive of the operation.
Topic archived. No new replies allowed.