|
-
June 18th, 2012, 01:38 AM
#1
Try to put own Class to creating by me <Template> like vector. unknow compile error
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 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.
My compiler error: compiler show my like windows message that name "MS Visual C++ Debag Library " and tell "Debag Error! Probram: c: \ bla lba \ debag \ 42a.exe Invalid allocation size 4229837429587 bytes... "
Please I really just want to see correction of code even without comments... Thanks in advance
Code:
// Array.h
// Templated Array class containging Ts
#include <sstream>
#include <iostream>
#ifndef Array_H
#define Array_H
template <class Type> class Array
{
private:
Type* m_data;
int m_size;
public:
Array<Type>();
Array<Type>(int new_size);
void SetElement(Type& type_object, int index);
const Type& GetElement(int index) const;
~Array();
};
#ifndef Array_cpp
#include "array.cpp"
#endif
#endif
//point1.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>
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()
{
//delete* m_data;
}
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 "point1.h"
#include <iostream>
#include <sstream>
#include "array.cpp"
using namespace std;
int main()
{
Point *p1 = new Point (1,12);
cout << endl;
Array<Point> arr1(2);
arr1.SetElement(*p1,0);
cout << endl;
arr1.GetElement(0) ;
delete p1;
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|