The assignment operator of Array1D < Array1D <T> >

Hi all,
I wrote a class Array1D which is of the form:
1
2
3
4
5
6
7
8
template <class T>
class Array1D
{
	int n;		//Length of the array
	T* pointer;	//Pointer to the data
public:
-Some methods here...
}


The copy constructor is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	Array1D (Array1D <T> const & A)
	{
		n = A.length();
		if (n != 0)
		{
			pointer = new T [n];
		} else
		{
			pointer = 0;
		};
		
		for (int k = 0; k < n; k++)
		{
			(pointer[k]) = A.at(k); //(**)
		};
	};

(the method at just returns the pointer[k])

When I used the class in the form Array1D < Array1D <double> > A (B) , I checked that the program did not invoke the assignment operator at (**), which was written as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	void operator = (Array1D <T> const & A)
	{
		cout << "copying" << endl;
		if (n != 0)
		{
			delete [] pointer;
		};
		n = A.length();
		if (n != 0)
		{
			pointer = new T [n];
		} else
		{
			pointer = 0;
		};
		for (int k = 0; k < n; k++)
		{
			pointer[k] = A.at(k);
		};
	};

As a result, all went wrong.
Can you please give me a hint to get around this?
Thank you very much!
Last edited on
closed account (D80DSL3A)
The problem description "all went wrong" is too vague. What specifically went wrong? Error codes?

The line pointer[k] = A.at(k); would invoke the = operator for the T object, not for the Array1D object.
You could check for a problem here by making the assignment directly as pointer[k] = A.pointer[k]; instead.

Are you sure about this Array1D < Array1D <double> > A (B); ?
It seems like it should be Array1D<double> A(B); instead.
Hi fun2code, thank you. It is my fault, I found the error, which is in different place.
Yes, I meant to use Array1D < Array1D <double> , the array1D of array1D. So the assignment evoked was of (Array1D <double>) =.
Again, thank you and sorry for bothering you with a wrong question.
Last edited on
closed account (D80DSL3A)
You're welcome. No problem. I see that Array1D < Array1D <double>> is sensible now. It is an array of arrays (a 2D array in essence). I'm not sure why I didn't see it before.
Glad you solved your own problem (this is the best way).
Last edited on
Topic archived. No new replies allowed.