Форум программистов, компьютерный форум CyberForum.ru

Static Varible And Function With <Template> - C++

Восстановить пароль Регистрация
 
Leeto
 Аватар для Leeto
7 / 7 / 0
Регистрация: 23.12.2011
Сообщений: 372
Записей в блоге: 1
18.07.2012, 19:00     Static Varible And Function With <Template> #1
Пытаюсь создать маленький проект чтобы лучше понять как static работает с шаблонами, но компилятор разрушил мои планы помогите пожалуйста разобраться с ошибками ) Заранее спасибо !

Ошибки
1>------ Build started: Project: 4.2b - Ex 1. Static Variable for Array Def Size. Templates, Configuration: Release Win32 ------
1> main.cpp
1>c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\array.cpp(40): error C2724: 'Array<Type>:efaultSize' : 'static' should not be used on member functions defined at file scope
1> with
1> [
1> Type=int
1> ]
1> c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\array.cpp(40) : while compiling class template member function 'void Array<Type>:efaultSize(int)'
1> with
1> [
1> Type=int
1> ]
1> main.cpp(77) : see reference to class template instantiation 'Array<Type>' being compiled
1> with
1> [
1> Type=int
1> ]
1>c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\array.cpp(32): error C2724: 'Array<Type>:efaultSize' : 'static' should not be used on member functions defined at file scope
1> with
1> [
1> Type=int
1> ]
1> c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\array.cpp(32) : while compiling class template member function 'int Array<Type>:efaultSize(void)'
1> with
1> [
1> Type=int
1> ]
1>c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\array.cpp(32): error C2724: 'Array<Type>:efaultSize' : 'static' should not be used on member functions defined at file scope
1> with
1> [
1> Type=double
1> ]
1> c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\array.cpp(32) : while compiling class template member function 'int Array<Type>:efaultSize(void)'
1> with
1> [
1> Type=double
1> ]
1> main.cpp(79) : see reference to class template instantiation 'Array<Type>' being compiled
1> with
1> [
1> Type=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



C++
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
//array.h
#ifndef Array_H
#define Array_H
 
template <class Type> //Remove the "=double" default parameter.
class Array
{
private:
  int m_size;
  Type* m_data; //m_data should be a pointer, since you want to allocate data to it
 // static int m_size_default;
 
//protected : 
    //static int m_size_default;
 
public:
 
static int m_size_default;
  Array();
  Array(int new_size);
  Array(const Array<Type>& ar);
  ~Array(); //Don't make your destructor virtual. There is no reason to do it.
 
  // Selectors 
  int Size() const {return m_size;};
  static int DefaultSize();
  static void DefaultSize(int newSize) ;
  
 
  const Type& Array<Type>::GetElement(int index) const;
  void Array<Type>::SetElement(const Type& type_object, int index);
  Array<Type>& operator=(const Array& ar); //Const correctness here.
  Type& Array<Type>::operator [] (int index);
  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 T>
int Array<T>::m_size_default = 10;
 
// default constructor 
template <typename T>
Array<T>::Array()
{
 
m_data = new T[m_size_default];
 
// iterate through array
for(int i=0; i<m_size_default; i++)
{
T temp;
m_data[i] = temp;
}
}
 
template <typename T>
static int Array<T>::DefaultSize() 
{ // line 32 
    
    return m_size_default;
}
 
 
template <typename T>
  static void Array<T>::DefaultSize(int newSize) 
  { // line 40 
      m_size_default = newSize; 
      
  };
 
 
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(const Array& ar) : m_data(0), m_size(0)
{
  if(!ar.m_data) {return;}
 
  Array tmp; //Copy construct into another temporary array, this way, if something throws, tmp will clean itself up.
 
  //Create the array
  tmp.m_data = new Type[ar.m_size];
  tmp.m_size = ar.m_size;
 
  //Copy the array elements
  for(int i = 0; i < tmp.m_size; ++i)
    {tmp.m_data[i] = ar.m_data[i];}
 
  //All done! swap into this!
  this->Swap(tmp);
}
 
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>
void Array<Type>::Swap(Array& ar)
{
  Type* data = m_data;
  int   size = m_size;
  m_data = ar.m_data;
  m_size = ar.m_size;
  ar.m_data = data;
  ar.m_size = size;
}
 
template <class Type> 
void Array<Type>::SetElement(const Type& type_object, int index)
{
 
        if (index >= m_size || index < 0 )
        { throw std:: out_of_range ("out of range error in void Array<Type>::SetElement");}
        m_data[index] = type_object;
        cout << "Set Element " << type_object  << endl;
 
    
}
template <class Type> 
const Type& Array<Type>::GetElement(int index) const
{
    
        if (index >= m_size || index < 0)
        { throw std::out_of_range ("Out of range error in void Array<Type>::GetElement");} 
 
    return m_data[index];
}
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];
}
 
 
 
#endif //Array_CPP
 
// ************ 
 
 
 
 
/*
Exercise 1: Static Variable for Array Default Size (optional)
 
Static variables, which are shared between all instances of a class, 
behave slightly different with templates. We are going to test this with a static variable in 
the Array class that indicates the default array size when using the default constructor.
Add a static data member to the Array class indicating the default size.
Initialise this static in the source file to a value.
Also add static functions to set and get the default size.
In the default constructor, use the static default size variable 
instead of a literal value to set the array size.
Test the following code in the main program:
Code (cpp):
Array<int> intArray1;
Array<int> intArray2;
Array<double> doubleArray;
 
cout<<intArray1.DefaultSize()<<endl;
cout<<intArray2.DefaultSize()<<endl;
cout<<doubleArray.DefaultSize()<<endl;
 
intArray1.DefaultSize(15);
 
cout<<intArray1.DefaultSize()<<endl;
cout<<intArray2.DefaultSize()<<endl;
cout<<doubleArray.DefaultSize()<<endl;
What values are printed? Can you explain the result?
*/
//
//
// QuantNet C++ course
// LEVEL 6. 4.2b - Advanced Templates
// [url]https://www.quantnet.com/threads/level-6-homework.7857/[/url]
// Exercise 1: Static Variable for Array Default Size (optional)
//
// Created by Denis Igoshev
//
// additional helping links: 
// [url]https://www.quantnet.com/threads/4-2b-exercise-1.9082/[/url]
// 
 
//main.cpp
        #include "point.h"
        #include <iostream>
        #include "array.cpp"
        #include <exception>
        using namespace std;
 
int main()
{
    Array<int> intArray1;
    Array<int> intArray2;
    Array<double> doubleArray;
 
    cout<<intArray1.DefaultSize()<<endl;
    cout<<intArray2.DefaultSize()<<endl;
    cout<<doubleArray.DefaultSize()<<endl; // line 57 
 
    intArray1.DefaultSize(15);
   
    cout<<intArray1.DefaultSize()<<endl;
    cout<<intArray2.DefaultSize()<<endl;
    cout<<doubleArray.DefaultSize()<<endl;
    
}
many thanks in advance
После регистрации реклама в сообщениях будет скрыта и будут доступны все возможности форума.
soon
 Аватар для soon
2536 / 1301 / 81
Регистрация: 09.05.2011
Сообщений: 3,086
Записей в блоге: 1
18.07.2012, 19:07     Static Varible And Function With <Template> #2
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Foo
{
    static int bar();
};
 
int Foo::bar()
{
    return 1;
}
 
int main()
{
 
    return 0;
}
Leeto
 Аватар для Leeto
7 / 7 / 0
Регистрация: 23.12.2011
Сообщений: 372
Записей в блоге: 1
19.07.2012, 19:47  [ТС]     Static Varible And Function With <Template> #3
Ошибки
1>------ Build started: Project: 4.2b - Ex 1. Static Variable for Array Def Size. Templates, Configuration: Release Win32 ------
1> Array.cpp
1>Array.cpp(10): error C3211: 'Array<Type>::m_size_default' : explicit specialization is using partial specialization syntax, use template <> instead
1> with
1> [
1> Type=int
1> ]
1> c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\Array.h(20) : see declaration of 'Array<Type>::m_size_default'
1> with
1> [
1> Type=int
1> ]
1>Array.cpp(13): error C3211: 'Array<Type>::m_size_default' : explicit specialization is using partial specialization syntax, use template <> instead
1> with
1> [
1> Type=double
1> ]
1> c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\Array.h(20) : see declaration of 'Array<Type>::m_size_default'
1> with
1> [
1> Type=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Цитата Сообщение от soon Посмотреть сообщение
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Foo
{
    static int bar();
};
 
int Foo::bar()
{
    return 1;
}
 
int main()
{
 
    return 0;
}
Я ничего не понял

Добавлено через 48 минут
Обновление :

C++
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
//array.h
#ifndef Array_H
#define Array_H
 
using namespace std;
 
template <class Type> //Remove the "=double" default parameter.
class Array
{
private:
  int m_size;
  Type* m_data; //m_data should be a pointer, since you want to allocate data to it
//  static int m_size_default;
 
//protected : 
    //static int m_size_default;
 
public:
 
  static int m_size_default; // line 20 
  Array();
  Array(int new_size);
  Array(const Array<Type>& ar);
  ~Array(); //Don't make your destructor virtual. There is no reason to do it.
 
  // Selectors 
  int Size() const {return m_size;};
  static int DefaultSize();
  static void DefaultSize(int newSize);
  
 
  const Type& GetElement(int index) const;
  void SetElement(const Type& type_object, int index);
  Array<Type>& operator=(const Array& ar); //Const correctness here.
  Type& operator [] (int index);
  void Swap(Array& ar);  
};
 
 
 
#endif
 
//array.cpp
#include "Array.h"
#include <sstream>
#include <iostream>
#include <exception>
using namespace std;
#ifndef Array_CPP
#define Array_CPP
 
template<class Type> // line 10 
int Array<int>::m_size_default = 10; // line 11
 
template<class Type> // line 13
int Array<double>::m_size_default = 10;
 
// default constructor 
    template <typename Type>
    Array<Type>::Array()
        {
 
    m_data = new Type[m_size_default];
 
            // iterate through array
            for(int i=0; i<m_size_default; i++)
                {
                    Type temp;
                    m_data[i] = temp;
                }
        }
template <typename Type>
static int Array<Type>::DefaultSize() 
{ // line 32 
    
    return m_size_default;
}
 
 
template <typename Type>
  static void Array<Type>::DefaultSize(int newSize)//: m_size_default(newSize)
  { // line 40 
     m_size_default = newSize; 
      
  }
 
 
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(const Array& ar) : m_data(0), m_size(0)
{
  if(!ar.m_data) {return;}
 
  Array tmp; //Copy construct into another temporary array, this way, if something throws, tmp will clean itself up.
 
  //Create the array
  tmp.m_data = new Type[ar.m_size];
  tmp.m_size = ar.m_size;
 
  //Copy the array elements
  for(int i = 0; i < tmp.m_size; ++i)
    {tmp.m_data[i] = ar.m_data[i];}
 
  //All done! swap into this!
  this->Swap(tmp);
}
 
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>
void Array<Type>::Swap(Array& ar)
{
  Type* data = m_data;
  int   size = m_size;
  m_data = ar.m_data;
  m_size = ar.m_size;
  ar.m_data = data;
  ar.m_size = size;
}
 
template <class Type> 
void Array<Type>::SetElement(const Type& type_object, int index)
{
 
        if (index >= m_size || index < 0 )
        { throw std:: out_of_range ("out of range error in void Array<Type>::SetElement");}
        m_data[index] = type_object;
        cout << "Set Element " << type_object  << endl;
 
    
}
template <class Type> 
const Type& Array<Type>::GetElement(int index) const
{
    
        if (index >= m_size || index < 0)
        { throw std::out_of_range ("Out of range error in void Array<Type>::GetElement");} 
 
    return m_data[index];
}
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];
}
 
 
 
#endif //Array_CPP
 
//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 + ")";
}
};
 
/*
Exercise 1: Static Variable for Array Default Size (optional)
 
Static variables, which are shared between all instances of a class, 
behave slightly different with templates. We are going to test this with a static variable in 
the Array class that indicates the default array size when using the default constructor.
Add a static data member to the Array class indicating the default size.
Initialise this static in the source file to a value.
Also add static functions to set and get the default size.
In the default constructor, use the static default size variable 
instead of a literal value to set the array size.
Test the following code in the main program:
Code (cpp):
Array<int> intArray1;
Array<int> intArray2;
Array<double> doubleArray;
 
cout<<intArray1.DefaultSize()<<endl;
cout<<intArray2.DefaultSize()<<endl;
cout<<doubleArray.DefaultSize()<<endl;
 
intArray1.DefaultSize(15);
 
cout<<intArray1.DefaultSize()<<endl;
cout<<intArray2.DefaultSize()<<endl;
cout<<doubleArray.DefaultSize()<<endl;
What values are printed? Can you explain the result?
*/
//
//
// QuantNet C++ course
// LEVEL 6. 4.2b - Advanced Templates
// [url]https://www.quantnet.com/threads/level-6-homework.7857/[/url]
// Exercise 1: Static Variable for Array Default Size (optional)
//
// Created by Denis Igoshev
//
// additional helping links: 
// [url]https://www.quantnet.com/threads/4-2b-exercise-1.9082/[/url]
// 
 
//main.cpp
        #include "Point.h"
        #include <iostream>
        #include "Array.cpp"
        #include <exception>
        using namespace std;
 
/*template<class Type>
int Array<int>::m_size_default = 10;
 
template<class Type>
int Array<double>::m_size_default = 10;*/
 
int main()
{
 
 
    Array<int> intArray1;
    Array<int> intArray2;
    Array<double> doubleArray;
 
    cout<<intArray1.DefaultSize()<<endl;
    cout<<intArray2.DefaultSize()<<endl;
    cout<<doubleArray.DefaultSize()<<endl; // line  
 
    intArray1.DefaultSize(15);
   
    cout<<intArray1.DefaultSize()<<endl;
    cout<<intArray2.DefaultSize()<<endl;
    cout<<doubleArray.DefaultSize()<<endl;
    
}
soon
 Аватар для soon
2536 / 1301 / 81
Регистрация: 09.05.2011
Сообщений: 3,086
Записей в блоге: 1
19.07.2012, 19:54     Static Varible And Function With <Template> #4
Цитата Сообщение от Leeto Посмотреть сообщение
Я ничего не понял
Я к тому, что реализация пишется без static.
Leeto
 Аватар для Leeto
7 / 7 / 0
Регистрация: 23.12.2011
Сообщений: 372
Записей в блоге: 1
19.07.2012, 20:00  [ТС]     Static Varible And Function With <Template> #5
Update of code :
C++
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
//array.h
#ifndef Array_H
#define Array_H
 
using namespace std;
 
template <class Type> //Remove the "=double" default parameter.
class Array
{
private:
  int m_size;
  Type* m_data; //m_data should be a pointer, since you want to allocate data to it
//  static int m_size_default;
 
//protected : 
    //static int m_size_default;
 
public:
 
  static int m_size_default; // line 20 
  Array();
  Array(int new_size);
  Array(const Array<Type>& ar);
  ~Array(); //Don't make your destructor virtual. There is no reason to do it.
 
  // Selectors 
  int Size() const {return m_size;};
  static int DefaultSize();
  static void DefaultSize(int newSize);
  
 
  const Type& GetElement(int index) const;
  void SetElement(const Type& type_object, int index);
  Array<Type>& operator=(const Array& ar); //Const correctness here.
  Type& operator [] (int index);
  void Swap(Array& ar);  
};
 
 
 
#endif
 
//array.cpp
#include "Array.h"
#include <sstream>
#include <iostream>
#include <exception>
using namespace std;
#ifndef Array_CPP
#define Array_CPP
 
template<class Type> // line 10 
int Array<int>::m_size_default = 10; // line 11
 
template<class Type> // line 13
int Array<double>::m_size_default = 10;
 
// default constructor 
    template <typename Type>
    Array<Type>::Array()
        {
 
    m_data = new Type[m_size_default];
 
            // iterate through array
            for(int i=0; i<m_size_default; i++)
                {
                    Type temp;
                    m_data[i] = temp;
                }
        }
template <typename Type>
 int Array<Type>::DefaultSize() 
{ // line 32 
    
    return m_size_default;
}
 
 
template <typename Type>
   void Array<Type>::DefaultSize(int newSize)//: m_size_default(newSize)
  { // line 40 
     m_size_default = newSize; 
      
  }
 
 
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(const Array& ar) : m_data(0), m_size(0)
{
  if(!ar.m_data) {return;}
 
  Array tmp; //Copy construct into another temporary array, this way, if something throws, tmp will clean itself up.
 
  //Create the array
  tmp.m_data = new Type[ar.m_size];
  tmp.m_size = ar.m_size;
 
  //Copy the array elements
  for(int i = 0; i < tmp.m_size; ++i)
    {tmp.m_data[i] = ar.m_data[i];}
 
  //All done! swap into this!
  this->Swap(tmp);
}
 
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>
void Array<Type>::Swap(Array& ar)
{
  Type* data = m_data;
  int   size = m_size;
  m_data = ar.m_data;
  m_size = ar.m_size;
  ar.m_data = data;
  ar.m_size = size;
}
 
template <class Type> 
void Array<Type>::SetElement(const Type& type_object, int index)
{
 
        if (index >= m_size || index < 0 )
        { throw std:: out_of_range ("out of range error in void Array<Type>::SetElement");}
        m_data[index] = type_object;
        cout << "Set Element " << type_object  << endl;
 
    
}
template <class Type> 
const Type& Array<Type>::GetElement(int index) const
{
    
        if (index >= m_size || index < 0)
        { throw std::out_of_range ("Out of range error in void Array<Type>::GetElement");} 
 
    return m_data[index];
}
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];
}
 
 
 
#endif //Array_CPP
 
/*
Exercise 1: Static Variable for Array Default Size (optional)
 
Static variables, which are shared between all instances of a class, 
behave slightly different with templates. We are going to test this with a static variable in 
the Array class that indicates the default array size when using the default constructor.
Add a static data member to the Array class indicating the default size.
Initialise this static in the source file to a value.
Also add static functions to set and get the default size.
In the default constructor, use the static default size variable 
instead of a literal value to set the array size.
Test the following code in the main program:
Code (cpp):
Array<int> intArray1;
Array<int> intArray2;
Array<double> doubleArray;
 
cout<<intArray1.DefaultSize()<<endl;
cout<<intArray2.DefaultSize()<<endl;
cout<<doubleArray.DefaultSize()<<endl;
 
intArray1.DefaultSize(15);
 
cout<<intArray1.DefaultSize()<<endl;
cout<<intArray2.DefaultSize()<<endl;
cout<<doubleArray.DefaultSize()<<endl;
What values are printed? Can you explain the result?
*/
//
//
// QuantNet C++ course
// LEVEL 6. 4.2b - Advanced Templates
// [url]https://www.quantnet.com/threads/level-6-homework.7857/[/url]
// Exercise 1: Static Variable for Array Default Size (optional)
//
// Created by Denis Igoshev
//
// additional helping links: 
// [url]https://www.quantnet.com/threads/4-2b-exercise-1.9082/[/url]
// 
 
//main.cpp
        #include "Point.h"
        #include <iostream>
        #include "Array.cpp"
        #include <exception>
        using namespace std;
 
/*template<class Type>
int Array<int>::m_size_default = 10;
 
template<class Type>
int Array<double>::m_size_default = 10;*/
 
int main()
{
 
 
    Array<int> intArray1;
    Array<int> intArray2;
    Array<double> doubleArray;
 
    cout<<intArray1.DefaultSize()<<endl;
    cout<<intArray2.DefaultSize()<<endl;
    cout<<doubleArray.DefaultSize()<<endl; // line  
 
    intArray1.DefaultSize(15);
   
    cout<<intArray1.DefaultSize()<<endl;
    cout<<intArray2.DefaultSize()<<endl;
    cout<<doubleArray.DefaultSize()<<endl;
    
}

Ошибки
1>------ Build started: Project: 4.2b - Ex 1. Static Variable for Array Def Size. Templates, Configuration: Release Win32 ------
1> main.cpp
1>c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\Array.cpp(10): error C3211: 'Array<Type>::m_size_default' : explicit specialization is using partial specialization syntax, use template <> instead
1> with
1> [
1> Type=int
1> ]
1> c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\Array.h(20) : see declaration of 'Array<Type>::m_size_default'
1> with
1> [
1> Type=int
1> ]
1>c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\Array.cpp(13): error C3211: 'Array<Type>::m_size_default' : explicit specialization is using partial specialization syntax, use template <> instead
1> with
1> [
1> Type=double
1> ]
1> c:\all my\с++\ha level 6\solution level 6\solution level 6\4.2b - ex1. static variable for array def size. templates\Array.h(20) : see declaration of 'Array<Type>::m_size_default'
1> with
1> [
1> Type=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
soon
 Аватар для soon
2536 / 1301 / 81
Регистрация: 09.05.2011
Сообщений: 3,086
Записей в блоге: 1
19.07.2012, 20:05     Static Varible And Function With <Template> #6
Цитата Сообщение от Leeto Посмотреть сообщение
template<class Type>
template <>
Leeto
 Аватар для Leeto
7 / 7 / 0
Регистрация: 23.12.2011
Сообщений: 372
Записей в блоге: 1
19.07.2012, 20:44  [ТС]     Static Varible And Function With <Template> #7
Цитата Сообщение от soon Посмотреть сообщение
template <>
где ? Я пробовал с m_defaul_size как то не очень

too few arguments
MoreAnswers
Эксперт
37091 / 29110 / 5898
Регистрация: 17.06.2006
Сообщений: 43,301
19.07.2012, 20:48     Static Varible And Function With <Template>
Еще ссылки по теме:

Ошибка компиляции: template-id does not match any template declaration C++
C++ Static function 'void leave(char *)' declared but not defined
'MyQueue' : use of class template requires template argument list C++

Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
soon
 Аватар для soon
2536 / 1301 / 81
Регистрация: 09.05.2011
Сообщений: 3,086
Записей в блоге: 1
19.07.2012, 20:48     Static Varible And Function With <Template> #8
Leeto, там, где выбивает ошибку. И too few arguments быть не может, я гарантирую это.
Yandex
Объявления
19.07.2012, 20:48     Static Varible And Function With <Template>
Ответ Создать тему
Опции темы

Текущее время: 12:41. Часовой пояс GMT +3.
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Рейтинг@Mail.ru