generic inheritance

Very simple idea:
There was <template> Array that take class Point, multiply to a factor and print this bunch of objects of class Point

Now I try to split Array to two template Array and NumericArray.
NumericArray derived only multiplication operator...

Pretty simple... but (((( :confused:

1>------ Build started: Project: HP3_4.2b_ex2_with_inheritance, Configuration: Release Win32 ------
1> NumericArray.cpp
1>c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(7): error C2143: syntax error : missing ',' before '<'
1> c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(15) : see reference to class template instantiation 'NumericArray<Type>' being compiled
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

h(7) and h(15) correspond open and close bracket of class

my code :

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//array.h

#ifndef Array_H
#define Array_H

template <class Type> //Remove the "=double" default parameter.
class Array
{
protected:
  int m_size;
  Type* m_data; //m_data should be a pointer, since you want to allocate data to it
 
public:
  Array();
  Array(int new_size);
  ~Array(); //Don't make your destructor virtual. There is no reason to do it.
  Array<Type>& operator=(const Array& ar); //Const correctness here.
  //Type& operator * (double factor) const;
  Array operator *(double factor) const;   // 
  Type& operator [] (int index);
  const Type& operator [] (int index) const;
  int Size() const;
};

#ifndef Array_cpp 
#include "array.cpp"
#endif
 
#endif //Array_CPP

//*******************
//*******************
//*******************
//*******************

//NumericArray.h

#ifndef NumericArray_H
#define NumericArray_H

template <class Type> //Remove the "=double" default parameter.
class NumericArray: public Array<Type>
{
	public: 
	NumericArray();
	NumericArray(int new_size);
	~NumericArray(); //Don't make your destructor virtual. There is no reason to do it.
	//NumericArray<Type>& operator=(const Array& ar); //Const correctness here.
	NumericArray operator *(double factor) const;   // 
};

#endif 


//*******************
//*******************
//*******************
//*******************

//point.h

#include "array.h"
#include <sstream>
#include <iostream>
using namespace std;


class Point
{
private:
    double m_x;                                
    double m_y;                                
public:
    // Constructors
    Point(): m_x(0), m_y(0) {};                            
    Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
    friend ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}
    //std::string Point::ToString(void) const                // create a string representation of a point
   string ToString() const
{
// create a string like: “Point(1.5, 3.9)”
      std::ostringstream os;
    os << m_x << " , " << m_y;
    std::string double_string = os.str();
 
    return "Point(" + double_string + ")";
 
}
    //Point Point::operator * (double factor) const;
    Point operator *(double factor) const;   
    //Point& Point::operator *= (double factor); 
    Point & operator *=(double factor);
};


//*******************
//*******************
//*******************
//*******************


//array.cpp
#include "Array.h"
#include <sstream>
#include <iostream>
#include <exception>
using namespace std;
#ifndef Array_CPP
#define Array_CPP


template <class Type>
Array<Type>::Array() : m_size(10), m_data(0) // странно получается, размер 10, а данных нет
{
}
template <class Type>
Array<Type>::Array(int new_size) : m_size(new_size), m_data(new Type[new_size])
{ 
}
 
template <class Type>
Array<Type>::~Array()
{
  //Technically, the if is not necessary
  if(m_data)
  {
    delete[] m_data;
    m_data = 0;
  }
 
  //Not necessary either, but just to be clean
  m_size = 0;
}
 
 
 
template <class Type>
Array<Type>& Array<Type>::operator=(const Array& ar)
{
  //Check self assign:
  if(this == &ar) {return *this;}
 
  Array<Type> copy(ar); //Create a copy of ar; If this fails, then *this will not be changed, and nothing will leak
 
  //Succeeded creating copy. Now we can put it inside this
  this->Swap(copy); //And then swap the copy into this!
 
  return *this;
}
 
 
 
 
 
template <class Type> 
Type& Array<Type>::operator [] (int index) 
{
    //cout << "Array [] operator" << endl;
 
    if (index > this->m_size)
    {
        cout << "i am hreeeee" << endl;
        return this->m_data[0];
    }
    return m_data[index];
}
 
template <class Type> 
 const Type& Array<Type>::operator [] (int index) const
 {
  //  cout << "Array [] operator" << endl;
 
    if (index > this->m_size)
    {
        cout << "i am hreeeee" << endl;
        return this->m_data[0];
    }
    return m_data[index];
}
 
template<class Type>
int Array<Type>::Size() const
{
    return this->m_size; 
}
 
/*template<class Type>
//Type& Array<Type>::operator * (double factor) const
Array<Type> Array<Type>::operator *(double factor) const
{
   Array<Type> output(Array<Type>::Size());
   for(int i=0; i<Array::Size(); i++)
   {
      output[i] = (*this)[i] * factor;
      //return output;
   }
   return output;
}
*/





 #endif //Array_CPP

//*******************
//*******************
//*******************
//*******************

//NumericArray.cpp
#include "NumericArray.h"
#include <sstream>
#include <iostream>
#include <exception>
using namespace std;

#ifndef NumericArray_CPP
#define NumericArray_CPP


template <class Type>
NumericArray<Type>::NumericArray() : m_size(10), m_data(0) // странно получается, размер 10, а данных нет
{
}
template <class Type>
NumericArray<Type>::NumericArray(int new_size) : m_size(new_size), m_data(new Type[new_size])
{ 
}
 
template <class Type>
NumericArray<Type>::~NumericArray()
{
  //Technically, the if is not necessary
  if(m_data)
  {
    delete[] m_data;
    m_data = 0;
  }
 
  //Not necessary either, but just to be clean
  m_size = 0;
}

template<class Type>
NumericArray<Type> NumericArray<Type>::operator *(double factor) const
{
   NumericArray<Type> output(NumericArray<Type>::Size());
   for(int i=0; i<Array::Size(); i++)
   {
      output[i] = (*this)[i] * factor;
      //return output;
   }
   return output;
}

 #endif //NumericArray_CPP


//*******************
//*******************
//*******************
//*******************


#include "Point.h"
#include <sstream>
#include <iostream>

using namespace std;
 
  Point Point::operator * (double factor) const
{
    return Point(m_x * factor, m_y * factor);
}
 
 
    Point& Point::operator *= (double factor)
    {
    Point tmp = (*this) * factor;
    *this = tmp;
 
    return *this;
    }


//****************
//****************
//****************


//main.cpp
		#include "point.h"
		#include <iostream>
		#include "array.cpp"
		#include <exception>
		#include "NumericArray.h"
		using namespace std;

 
int main()
{
              //Create two Point arrays and test the operators
    Array<Point> pArray1(5);
    Array<Point> pArray2(5);
    //initialize
    for(int i=0; i<pArray1.Size(); i++) pArray1[i] = Point(i, i);
    for(int i=0; i<pArray2.Size(); i++) pArray2[i] = Point(2*i, 2*i);
 
    //Numeric Array's operations not working for Point objects
 
 
    cout << "times PointArray1 by 3 and print out the new array: "<< endl;
    Array<Point> answ1 = pArray1 * 3;
    
    for(int i=0; i<answ1.Size(); i++){
        cout << answ1[i] << endl;
 
    }
 
 
    
}

//*******************
//*******************
//*******************
//******************* 


Many many thanks in advance !
I made this code work but from condition I should call operator * from NumericalArray not from Array
and relationship between those two template should be hierarchy (ISA if I not mistaken )
How should I re write code???


Once again this is working code :

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//array.h //array.h //array.h //array.h //array.h //array.h //array.h //array.h //array.h //array.h 

#ifndef Array_H
#define Array_H

template <class Type> //Remove the "=double" default parameter.
class Array
{
protected:
  int m_size;
  Type* m_data; //m_data should be a pointer, since you want to allocate data to it
 
public:
  Array();
  Array(int new_size);
 virtual ~Array(); //Don't make your destructor virtual. There is no reason to do it.
  Array<Type>& operator=(const Array& ar); //Const correctness here.
  //Type& operator * (double factor) const;
  Array operator *(double factor) const;   // 
  Type& operator [] (int index);
  const Type& operator [] (int index) const;
  int Size() const;
};

#ifndef Array_cpp 
#include "array.cpp"
#endif
 
#endif //Array_CPP

//*******************
//*******************
//*******************
//*******************

//NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h 
#include "array.h"

#ifndef NumericArray_H
#define NumericArray_H

template <class Type> //Remove the "=double" default parameter.
class NumericArray: public Array<Type>
{
	public: 
	NumericArray();
	NumericArray(int new_size);
	~NumericArray(); //Don't make your destructor virtual. There is no reason to do it.
	//NumericArray<Type>& operator=(const Array& ar); //Const correctness here.
	NumericArray operator *(double factor) const;   // 
};

#ifndef NumericArray_cpp 
#include "NumericArray.cpp"
#endif

#endif // NumericArray


//*******************
//*******************
//*******************
//*******************


//point.h //point.h //point.h //point.h //point.h //point.h //point.h //point.h //point.h //point.h 

#include "array.h"
#include <sstream>
#include <iostream>
using namespace std;


class Point
{
private:
    double m_x;                                
    double m_y;                                
public:
    // Constructors
    Point(): m_x(0), m_y(0) {};                            
    Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
    friend ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}
    //std::string Point::ToString(void) const                // create a string representation of a point
   string ToString() const
{
// create a string like: “Point(1.5, 3.9)”
      std::ostringstream os;
    os << m_x << " , " << m_y;
    std::string double_string = os.str();
 
    return "Point(" + double_string + ")";
 
}
    //Point Point::operator * (double factor) const;
    Point operator *(double factor) const;   
    //Point& Point::operator *= (double factor); 
    Point & operator *=(double factor);
};


//*******************
//*******************
//*******************
//*******************

//array.cpp //array.cpp //array.cpp //array.cpp //array.cpp //array.cpp //array.cpp //array.cpp 
#include "Array.h"
#include <sstream>
#include <iostream>
#include <exception>
using namespace std;
#ifndef Array_CPP
#define Array_CPP


template <class Type>
Array<Type>::Array() : m_size(10), m_data(0) // странно получается, размер 10, а данных нет
{
}
template <class Type>
Array<Type>::Array(int new_size) : m_size(new_size), m_data(new Type[new_size])
{ 
}
 
template <class Type>
Array<Type>::~Array()
{
  //Technically, the if is not necessary
  if(m_data)
  {
    delete[] m_data;
    m_data = 0;
  }
 
  //Not necessary either, but just to be clean
  m_size = 0;
}
 
 
 
template <class Type>
Array<Type>& Array<Type>::operator=(const Array& ar)
{
  //Check self assign:
  if(this == &ar) {return *this;}
 
  Array<Type> copy(ar); //Create a copy of ar; If this fails, then *this will not be changed, and nothing will leak
 
  //Succeeded creating copy. Now we can put it inside this
  this->Swap(copy); //And then swap the copy into this!
 
  return *this;
}
 
 
 
 
 
template <class Type> 
Type& Array<Type>::operator [] (int index) 
{
    //cout << "Array [] operator" << endl;
 
    if (index > this->m_size)
    {
        cout << "i am hreeeee" << endl;
        return this->m_data[0];
    }
    return m_data[index];
}
 
template <class Type> 
 const Type& Array<Type>::operator [] (int index) const
 {
  //  cout << "Array [] operator" << endl;
 
    if (index > this->m_size)
    {
        cout << "i am hreeeee" << endl;
        return this->m_data[0];
    }
    return m_data[index];
}
 
template<class Type>
int Array<Type>::Size() const
{
    return this->m_size; 
}
 
template<class Type>
//Type& Array<Type>::operator * (double factor) const
Array<Type> Array<Type>::operator *(double factor) const
{
   Array<Type> output(Array<Type>::Size());
   for(int i=0; i<Array::Size(); i++)
   {
      output[i] = (*this)[i] * factor;
      //return output;
   }
   return output;
}






 #endif //Array_CPP

//*******************
//*******************
//*******************
//*******************

//NumericArray.cpp //NumericArray.cpp //NumericArray.cpp //NumericArray.cpp //NumericArray.cpp //NumericArray.cpp //NumericArray.cpp //NumericArray.cpp 
#include "NumericArray.h"
#include <sstream>
#include <iostream>
#include <exception>
#include "array.h"
using namespace std;

#ifndef NumericArray_CPP
#define NumericArray_CPP
#include "array.h"

template <class Type>
NumericArray<Type>::NumericArray() : m_size(10), m_data(0) // default filling of array is zeros!
{
}
template <class Type>
NumericArray<Type>::NumericArray(int new_size) : m_size(new_size), m_data(new Type[new_size])
{ 
}
 
template <class Type>
NumericArray<Type>::~NumericArray()
{
  //Technically, the if is not necessary
  if(m_data)
  {
    delete[] m_data;
    m_data = 0;
  }
 
  //Not necessary either, but just to be clean
  m_size = 0;
}

template<class Type>
NumericArray<Type> NumericArray<Type>::operator *(double factor) const
{
   NumericArray<Type> output(NumericArray<Type>::Size());
   for(int i=0; i<Array::Size(); i++)
   {
      output[i] = (*this)[i] * factor;
      //return output;
   }
   return output;
}

 #endif //NumericArray_CPP


//*******************
//*******************
//*******************
//*******************


// Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp 
#include "Point.h"
#include <sstream>
#include <iostream>

using namespace std;
 
  Point Point::operator * (double factor) const
{
    return Point(m_x * factor, m_y * factor);
}
 
 
    Point& Point::operator *= (double factor)
    {
    Point tmp = (*this) * factor;
    *this = tmp;
 
    return *this;
    }


//****************
//****************
//****************


//main.cpp //main.cpp //main.cpp //main.cpp //main.cpp //main.cpp //main.cpp //main.cpp 

		#include "point.h"
		#include <iostream>
		#include "array.cpp"
		#include <exception>
		#include "NumericArray.h"
		using namespace std;

 
int main()
{
              //Create two Point arrays and test the operators
    Array<Point> pArray1(5);
    Array<Point> pArray2(5);
    //initialize
    for(int i=0; i<pArray1.Size(); i++) pArray1[i] = Point(i, i);
    for(int i=0; i<pArray2.Size(); i++) pArray2[i] = Point(2*i, 2*i);
 
    //Numeric Array's operations not working for Point objects
 
 
    cout << "times PointArray1 by 3 and print out the new array: "<< endl;
    Array<Point> answ1 = pArray1 * 3;
    
    for(int i=0; i<answ1.Size(); i++){
        cout << answ1[i] << endl;
 
    }
 
 
    
}

//*******************
//*******************
//*******************
//******************* 
okey I'm done previous mistake...
"'m_data' is not a base or member" BUT WHY ???
I'm realize inherent and made data member on parents template protected (not private)... What is wrong again ?

PS this is realization of assignment operator in NumerArray.h

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//array.h //array.h //array.h //array.h //array.h //array.h //array.h //array.h //array.h //array.h 

#ifndef ARRAY_H
#define ARRAY_H

template <class Type> 
class Array

{
protected:
  int m_size;
  Type* m_data; //m_data should be a pointer, since you want to allocate data to it
 
public:
  Array();
  Array(int new_size);
  virtual ~Array(); //Are there reason making destructor virtual?
  Array<Type>& operator=(const Array& ar); //Const correctness here.
  Type& operator [] (int index);
  const Type& operator [] (int index) const;
  int Size() const;
};

#ifndef ARRAY_CPP 
#include "array.cpp"
#endif
 
#endif //Array_CPP

//*******************
//*******************
//*******************
//*******************

//NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h //NumericArray.h 


#ifndef NUMERICARRAY_H
#define NUMERICARRAY_H

#include "array.h"

template <class Type> 
class NumericArray: public Array<Type>

{
    public: 
        NumericArray();
        NumericArray(int new_size);
       ~NumericArray(); 
        NumericArray<Type>& operator=(const NumericArray<Type>& ar); 
        NumericArray<Type>& operator *(double factor) const;    
};

template <class Type>
NumericArray<Type>::NumericArray() : m_size(10), m_data(0) // default filling of array is zeros!
{
}



template <class Type>
NumericArray<Type>::NumericArray(int new_size) : m_size(new_size), m_data(new Type[new_size])
{ 
}
 
template <class Type>
NumericArray<Type>& NumericArray<Type>::operator=(const NumericArray& ar) 
{
  //Check self assign:
  if(this == &ar) {return *this;}
  Array<Type> copy(ar); //Create a copy of ar; If this fails, then *this will not be changed, and nothing will leak
  //Succeeded creating copy. Now we can put it inside this
  this->Swap(copy); //And then swap the copy into this!
 return *this;
}

template <class Type>
NumericArray<Type>::~NumericArray()
{
  //Technically, the if is not necessary
  if(m_data)
  {
    delete[] m_data;
    m_data = 0;
  }
  //Not necessary either, but just to be clean
  m_size = 0;
}

template<class Type>
NumericArray<Type>& NumericArray<Type>::operator *(double factor) const
{
   NumericArray<Type> output(Array<Type>::Size());
   for(int i=0; i<Array::Size(); i++)
   {
      output[i] = (*this)[i] * factor;
      //return output;
   }
   return output;
}



#endif //NumericArray_h 


//*******************
//*******************
//*******************
//*******************


//point.h //point.h //point.h //point.h //point.h //point.h //point.h //point.h //point.h //point.h 


#ifndef POINT_H
#define POINT_H

#include "array.h"
#include <sstream>
#include <iostream>

using namespace std;
class Point
{
protected:
    double m_x;                                
    double m_y;                                
public:
    // Constructors
    Point(): m_x(0), m_y(0) {};                            
    Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
    friend ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}
    //std::string Point::ToString(void) const                // create a string representation of a point
   string ToString() const
{
// create a string like: “Point(1.5, 3.9)”
      std::ostringstream os;
    os << m_x << " , " << m_y;
    std::string double_string = os.str();
 
    return "Point(" + double_string + ")";
 
}
    //Point Point::operator * (double factor) const;
    Point operator *(double factor) const;   
    //Point& Point::operator *= (double factor); 
    Point & operator *=(double factor);
};

#endif //Point_h

//*******************
//*******************
//*******************
//*******************


//array.cpp //array.cpp //array.cpp //array.cpp //array.cpp //array.cpp //array.cpp //array.cpp 

#ifndef Array_CPP
#define Array_CPP

#include "Array.h"
#include <sstream>
#include <iostream>
//#include <exception>
using namespace std;



template <class Type>
Array<Type>::Array() : m_size(10), m_data(0) // 
{
}
template <class Type>
Array<Type>::Array(int new_size) : m_size(new_size), m_data(new Type[new_size])
{ 
}
 
template <class Type>
Array<Type>::~Array()
{
  //Technically, the if is not necessary
  if(m_data)
  {
    delete[] m_data;
    m_data = 0;
  }
 
  //Not necessary either, but just to be clean
  m_size = 0;
}
template <class Type>
Array<Type>& Array<Type>::operator=(const Array& ar)
{
  //Check self assign:
  if(this == &ar) {return *this;}
  Array<Type> copy(ar); //Create a copy of ar; If this fails, then *this will not be changed, and nothing will leak
  //Succeeded creating copy. Now we can put it inside this
  this->Swap(copy); //And then swap the copy into this!
  return *this;
}
 
 
 
 
 
template <class Type> 
Type& Array<Type>::operator [] (int index) 
{
    if (index > this->m_size)
    {
        cout << "i am hreeeee" << endl;
        return this->m_data[0];
    }
    return m_data[index];
}
 
template <class Type> 
 const Type& Array<Type>::operator [] (int index) const
 {
  //  cout << "Array [] operator" << endl;
 
    if (index > this->m_size)
    {
        cout << "i am hreeeee" << endl;
        return this->m_data[0];
    }
    return m_data[index];
}
 
template<class Type>
int Array<Type>::Size() const
{
    return this->m_size; 
}
 


/*template<class Type>
//Type& Array<Type>::operator * (double factor) const
Array<Type> Array<Type>::operator *(double factor) const
{
   Array<Type> output(Array<Type>::Size());
   for(int i=0; i<Array::Size(); i++)
   {
      output[i] = (*this)[i] * factor;
      //return output;
   }
   return output;
}*/


 #endif //Array_CPP

//*******************
//*******************
//*******************
//*******************


// Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp // Point.cpp 


#include "Point.h"
#include <sstream>
#include <iostream>
using namespace std;
 
  Point Point::operator * (double factor) const
{
    return Point(m_x * factor, m_y * factor);
}

    Point& Point::operator *= (double factor)
		{
    Point tmp = (*this) * factor;
    *this = tmp;
    return *this;
		}


//****************
//****************
//****************


//main.cpp //main.cpp //main.cpp //main.cpp //main.cpp //main.cpp //main.cpp //main.cpp 

		#include "point.h"
		#include "Array.h"
		#include "array.cpp"
		#include <exception>
		#include "NumericArray.h"
		#include <iostream>

		using namespace std;
int main()
{
              //Create two Point arrays and test the operators
    NumericArray<Point> pArray1(5);
    NumericArray<Point> pArray2(5);
    //initialize
    for(int i=0; i<pArray1.Size(); i++) pArray1[i] = Point(i, i);
    for(int i=0; i<pArray2.Size(); i++) pArray2[i] = Point(2*i, 2*i);
 
    //Numeric Array's operations not working for Point objects
 
 
    cout << "times PointArray1 by 3 and print out the new array: "<< endl;
    NumericArray<Point> answ1 = pArray1 * 3;
    
    for(int i=0; i<answ1.Size(); i++)
	{
        cout << answ1[i] << endl;
    }
}

//*******************
//*******************
//*******************
//*******************
Last edited on
What compiler said

1>------ Build started: Project: HP3_4.2b_ex2_with_inheritance, Configuration: Release Win32 ------
1> main.cpp
1>c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(30): error C2614: 'NumericArray<Type>' : illegal member initialization: 'm_data' is not a base or member
1> with
1> [
1> Type=Point
1> ]
1> c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(29) : while compiling class template member function 'NumericArray<Type>::NumericArray(int)'
1> with
1> [
1> Type=Point
1> ]
1> main.cpp(14) : see reference to class template instantiation 'NumericArray<Type>' being compiled
1> with
1> [
1> Type=Point
1> ]
1>c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(30): error C2614: 'NumericArray<Type>' : illegal member initialization: 'm_size' is not a base or member
1> with
1> [
1> Type=Point
1> ]
1>c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(66): warning C4172: returning address of local variable or temporary
1> c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(59) : while compiling class template member function 'NumericArray<Type> &NumericArray<Type>::operator *(double) const'
1> with
1> [
1> Type=Point
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Topic archived. No new replies allowed.