I try to create small project in order to better understand how key word static works with templates . However some compiles errors crush my plan. Could you help me resolve compile errors:
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>::DefaultSize' : '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>::DefaultSize(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>::DefaultSize' : '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>::DefaultSize(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>::DefaultSize' : '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>::DefaultSize(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 ==========
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
|
//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
... // limition of 9000 char.
};
//*****************
//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
// https://www.quantnet.com/threads/level-6-homework.7857/
// Exercise 1: Static Variable for Array Default Size (optional)
//
// Created by Denis Igoshev
//
// additional helping links:
// https://www.quantnet.com/threads/4-2b-exercise-1.9082/
//
//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