This is study project because of this I don't use code from STL vector.
I want to create some program that will be do next:
1. Create object of class for example Point (class that has x,y coordinate)
2. Create object of <template> name "Array" with fix amount of elements.
3. Put object of class Point to Array on needed me place that represent index of Array. Using fucntion SetElement
4. Using function GetElement I want to this this "Point" object on screen.
Very simple, please help !!! What I did wrong ???
5. I create object of "Point " dynamically because of condition of study project.
Please I really just want to see correction of code even without comments...
Thanks in advance
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
|
// Array.h
// Templated Array class containging Ts
#include <sstream>
#include <iostream>
#ifndef Array_H
#define Array_H
template <class Type=double> class Array
{
private:
int m_size;
Type* m_data;
public:
Array();
Array(int new_size);
void SetElement(Type& type_object, int index);
const Type& GetElement(int index) const;
};
#ifndef Array_cpp
#include "array.cpp"
#endif
#endif
//*********************
//array.cpp
#include "array.h"
#include <sstream>
#include <iostream>
using namespace std;
#ifndef Array_CPP
#define Array_CPP
template <class Type>
Array<Type>::Array()
{
m_size = 10;
m_data = new Type[m_size];
}
template <class Type>
Array<Type>::Array(int new_size)
{
new_size = m_size;
m_data = new Type[m_size];
}
template <class Type>
void Array<Type>::SetElement(Type& type_object, int index)
{
try
{
if (index >= m_size){throw 11;}
m_data[index] = type_object;
cout << "Set Element " << type_object << endl;
}
catch (int x )
{
cout << "ERROR" << x << " ignore the set, because index of element bigger then massive size"<< endl;
}
}
template <class Type>
const Type& Array<Type>::GetElement(int index) const
{
try
{
if (index > m_size || index < 0){ throw 1;}
}
catch (double x)
{
cout << "index incorrect, index too big or too small " << x << endl;
}
return m_data[index];
}
#endif Array_CPP
//********************
//main.cpp
#include <iostream>
#include <sstream>
#include "array.h"
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) {};
};
int main()
{
Point *p1 = new Point (1,12);
cout << endl;
Array<Point> arr1[1];
arr1[1].SetElement(*p1,0);
cout << endl;
arr1[1].GetElement(0) ;
delete p1;
return 0;
}
|
Compiler errors give a lot of words related with streams or something for examlple :
_Traits=std::char_traits<char>
1> ]
1> c:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(467): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1> with
1>------ Build started: Project: HP_4.2a, Configuration: Release Win32 ------
1> TestArray.cpp
1>c:\all my\с++\ha level 6\solution\level 6\hp_4.2a\array.cpp(31): error C2679:
1> TestArray.cpp(23) : see reference to class template instantiation