Template. Numerical Array.

I what to implement to my Template operator * .
So, Very simple idea.
There is <Template> Array which purpose is container like vector for classes
There is class Point, each object of contain two coordinate x and y.
So,
1. I wanna fill Array with objects from Point class
2. Multiply each objects from this vector to a factor
3. And print all this bunch of objects ()...

Compile error :
1>------ Build started: Project: HP_4.2b_Ex2, Configuration: Release Win32 ------
1> main.cpp
1>main.cpp(21): error C2440: 'initializing' : cannot convert from 'Point' to 'Array<Type>'
1> with
1> [
1> Type=Point
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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
//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(const Array<Type>& ar);
  ~Array(); //Don't make your destructor virtual. There is no reason to do it.
  Type& operator * (double factor) const;
  Type& operator [] (int index);
  int Size() const;
  void Swap(Array& ar);  
};



#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
{
// 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 + ")";
}
};

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

//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) 
{
}
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> 
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>
int Array<Type>::Size() const
{
	return this->m_size; 
}

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


#endif //Array_CPP

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

//main.cpp
		#include "point.h"
		
		#include <iostream>
		#include "array.cpp"
		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;

    }
 

	
}
Here

for(int i=0; i<pArray1.Size(); i++) pArray1[i] = Point(i, i);

you are trying to assign object of type Point to an object of type Array<Point>

Sorry I am wrong I thought that this Array<Point> pArray1(5); is the definition of an array.

I see an error in the following definition


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

Your template parameter is Type but in the statement

output[i] = Array<T>::operator[](i) * factor;

you use template parameter T
Last edited on
O my god... so silly mistake... I re - wrote already but compiler again
give me the same error :

1>------ Build started: Project: HP_4.2b_Ex2, Configuration: Release Win32 ------
1> main.cpp
1>main.cpp(23): error C2440: 'initializing' : cannot convert from 'Point' to 'Array<Type>'
1> with
1> [
1> Type=Point
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


main.cpp(23): Array<Point> answ1 = pArray1 * 3;
under red line: pArray1
and pop -up helper tell that : Error: no suitable user defined conversion from "Point " to Array<Point> exist
Your operator is defined as

Type& operator * (double factor) const;

In case if the Type is the Point then you are trying to assign an object of type Point to an object of type Array<Point>:

Array<Point> answ1 = pArray1 * 3;
how should I re-write my code ... Please help

last edition:

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
//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;
  Type& operator [] (int index);
  const Type& operator [] (int index) const;
  int Size() 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
{
// 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& 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) 
{
}
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> output(Array<Type>::Size());
for(int i=0; i<Array::Size(); i++)
	{
output[i] = (*this)[i] * factor;
return output;
	}

}

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

#endif //Array_CPP


#include "array.h"
#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"
		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;

    }
 

	
}


1>------ Build started: Project: HP_4.2b_Ex2, Configuration: Release Win32 ------
1> Array.cpp
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

1>------ Build started: Project: HP_4.2b_Ex2, Configuration: Release Win32 ------
1> Point.cpp
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


1>------ Build started: Project: HP_4.2b_Ex2, Configuration: Release Win32 ------
1> main.cpp
1>main.cpp(20): error C2440: 'initializing' : cannot convert from 'Point' to 'Array<Type>'
1> with
1> [
1> Type=Point
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
I implement your ideas !!! Everything fine !!!
could you check on other silly mistakes please


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
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;   // я это имел ввиду (про header)
  Type& operator [] (int index);
  const Type& operator [] (int index) const;
  int Size() const;
};
//****************
//****************
//****************
 
#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& 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
 
 
//#include "array.h"
//#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"
//        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;
 
    }
 
 
    
}
Topic archived. No new replies allowed.