CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. CHO
Junior Member
Join Date
Dec 2010
Posts
16

the operator+ and operator^ are not working. Can anyone correct?.

Code:
```#include <iostream>
#include <algorithm>

using namespace std;

template <class T>
class Array2D
{
T* data;
int w, h;
public:
Array2D(int X, int Y) : w(X), h(Y)
{
data = new T[w*h];
}

~Array2D()
{
if (data != NULL)
{
delete [] data;
data = NULL;
}
}

T& operator ()(int Col, int Row)
{
return data[Row*w+Col];
}

Array2D operator+( Array2D& m)
{
int w = m.width();
int h = m.height();
Array2D theArray2D(w,h);
for (int r=0;r<h;r++)
{
for (int c=0;c<w;c++)
{
theArray2D(c,r)=data[c*w+r]+m(c,r);
}
}
return theArray2D;
}

Array2D operator^( cont T& t)
{
//need to be filled
}

int width()
{return w;}

int height()
{return h;}

};

int main( )
{
int c = 5;
int r = 6;

Array2D<int> b(c,r);
Array2D<int> b1(c,r);

for(int j=0; j<r; j++)
{
for(int i=0; i<c; i++)
{
b(i,j) = i+j;
b1(i,j) = i+j;
}
}

Array2D<int> a1 = b + b1;
Array2D<int> a2 = b ^ 2;

for(int j=0; j<r; j++)
{
for(int i=0; i<c; i++)
{
std::cout << a1(i,j) << "\t";
}
std::cout << "\n";
}

return 0;
}```

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Originally Posted by CHO
the operator+ and operator^ are not working. Can anyone correct?.
What do you mean by "not working"?

Secondly, for what reason are you overloading operator ^? If you're overloading it to do powers -- forget it. The "^" precedence level is incompatible with it being used for power operations.

Regards,

Paul McKenzie

3. CHO
Junior Member
Join Date
Dec 2010
Posts
16

these are the results from a1(i,j) , operator+.:

-572662307 1 0 0 -33686019
-572662307 524295 786688 3233816 0
273774084 177 8 2 134
-33686019 3232304 3232360 -33686019 -572662307
458778 -572719104 3211848 3211848 -572662307
-572662307 -572662307 -572662307 -572662307 -572662307

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Originally Posted by CHO
these are the results from a1(i,j) , operator+.:

-572662307 1 0 0 -33686019
-572662307 524295 786688 3233816 0
273774084 177 8 2 134
-33686019 3232304 3232360 -33686019 -572662307
458778 -572719104 3211848 3211848 -572662307
-572662307 -572662307 -572662307 -572662307 -572662307
Before asking one of us to debug your code, have you debugged your code? Every programmer makes mistakes, and debugging your own code is part and parcel of learning how to program.
You don't. You write a function to do it.

Notice that C++ has a pow() function, and does not use ^ for powers for the reasons I described.

Regards,

Paul McKenzie

5. CHO
Junior Member
Join Date
Dec 2010
Posts
16

6. CHO
Junior Member
Join Date
Dec 2010
Posts
16

value of b1 changes after the + operation. Tried to prevent that , but no luck.

Code:
```#include <iostream>
#include <algorithm>

using namespace std;

template <class T>
class Array2D
{
T* data;
int w, h;
public:
Array2D(int X, int Y) : w(X), h(Y)
{
data = new T[w*h];
}

~Array2D()
{
if (data != NULL)
{
delete [] data;
data = NULL;
}
}

T& operator ()(int Col, int Row)
{
return data[Row*w+Col];
}

Array2D<T>& operator+( Array2D<T>& m)
{
//Array2D<int> c(w,h);

//Array2D<int>& result = c;

Array2D& result = m;
for (int r=0;r<h;r++)
{
for (int c=0;c<w;c++)
{
result(c,r) = (*this)(c,r)+m(c,r);
}
}
return result;
}

int width()
{return w;}

int height()
{return h;}

};

int main( )
{
int c = 5;
int r = 6;

Array2D<int> b(c,r);
Array2D<int> b1(c,r);

for(int j=0; j<r; j++)
{
for(int i=0; i<c; i++)
{
b(i,j) = 2;
b1(i,j) = 1;
}
}

//value of b1 changes after this line. Tried to prevent that , but no luck.
Array2D<int>& a1 = b + b1;

for(int j=0; j<r; j++)
{
for(int i=0; i<c; i++)
{
std::cout << a1(i,j) << "\t";
}
std::cout << "\n";
}

return 0;
}```

7. CHO
Junior Member
Join Date
Dec 2010
Posts
16

Code:
```    Array2D<T>& operator+( Array2D<T>& m)
{
Array2D<int> *n = new Array2D<int>(w,h);

Array2D<int>& result = *n;
for (int r=0;r<h;r++)
{
for (int c=0;c<w;c++)
{
result(c,r) = (*this)(c,r)+m(c,r);
}
}
return result;
}```
it works now. if there is a better way to do this, let me know.

8. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,712

1. I see a "new" , but no delete. You will have a memory leak

2. You do memory allocation. You will need to code up a proper
copy constructor and assignment operator

3. Normally, you do not make operator + a member variable.
You make operator += a member (returning a reference to *this

4. You make operator + a non member function, coded in terms
of operator += and the copy constructor.

9. CHO
Junior Member
Join Date
Dec 2010
Posts
16

I am new to operator overloading. If anyone can make the changes to the class and the main(), to see, it be useful.

10. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,712

Here is an example (non templated ... "1D" array).

I did not check everything ... but this will give you an idea ...

Code:
```#include <iostream>
#include <algorithm>

using namespace std;

class Array
{
int * pArray;

int size;

public:

Array(int dim = 10) : size(dim)
{
pArray = new int [size];
}

Array (const Array & rhs)
{
pArray = new int [rhs.size];
size = rhs.size;

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

void Swap (Array & rhs) throw ()
{
std::swap(size,rhs.size);
std::swap(pArray,rhs.pArray);
}

Array & operator = (const Array & rhs)
{
Array temp(rhs);
Swap(temp);
return *this;
}

Array & operator += (const Array & rhs)
{
if (size > rhs.size)
{
// you need to decide what to do here
}
else
{
for (int i=0; i<size; ++i)
{
pArray[i] += rhs.pArray[i];
}
}

return *this;
}

size_t Size() { return size; }

const int & operator [] (int i) const
{
return pArray[i];
}

int & operator [] (int i)
{
return pArray[i];
}

~Array()
{
delete [] pArray;
}
};

Array operator + (const Array & lhs , const Array & rhs)
{
Array temp(lhs);
return temp += rhs;
}

int main()
{
Array a(3);
Array b(3);
Array c(3);

for (int i=0; i<a.Size(); ++i)
{
a[i] = 1;
b[i] = 6;
}

c = a + b;

for (int i=0; i<c.Size(); ++i)
{
cout << c[i] << "\n";
}

return 0;
}```
Last edited by Philip Nicoletti; December 30th, 2010 at 01:34 PM.

11. Member +
Join Date
Apr 2008
Posts
726

Originally Posted by CHO
Code:
```    Array2D<T>& operator+( Array2D<T>& m)
{
Array2D<int> *n = new Array2D<int>(w,h);

Array2D<int>& result = *n;
for (int r=0;r<h;r++)
{
for (int c=0;c<w;c++)
{
result(c,r) = (*this)(c,r)+m(c,r);
}
}
return result;
}```
it works now. if there is a better way to do this, let me know.
operator+ should not change either argument, therefore it should return an object that is neither of the arguments i.e. it should not return a reference, but a new object, like philip has shown.

12. Senior Member
Join Date
Nov 2006
Location
Essen, Germany
Posts
1,344

1) You can eliminate the necessity of a copy constructor/assignment operator by using std::vector instead of a C-style array.

2) binary operators (i.e. operator+) should be implemented as non-member functions.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•