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 ???
Please I really just want to see correction of code even without comments...
Thanks in advance
// 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
PS I create object of "Point " dynamically because of condition of study project.

New Topic/Question
Reply



MultiQuote






|