Try to implement exception handline... Strange mistake

In very end moment before compiler done his work completely, a message of error occur that
say : "
Windows has triggered a breakpoint in _project name_.exe.

This may be due to a corruption of the heap, which indicates a bug in _project name_.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while _project name_.exe has focus.

The output window may have more diagnostic information. "
Please Help. Many thanks in advance.
Before I implement try... catch this message did not appear
my code :

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
//array.h
#ifndef Array_H
#define Array_H
#include "point.h"
#include <string>
using namespace std;
 
class Array
{
private:
    Point* m_data;                         // Dynamic Array of Point pointers.    
    int m_size;                            // Size of Dynamic Array.                                             
public: 		
    Array();                         // Default constructor
    Array(const int new_size);       // constructor with size argument.
    Array(const Array& ar);          // Copy constructor
    ~Array();                        // Destructor		
    int Size();                      // Get the size of the m_data pointer Array	
    void SetElement(const Point& pt, int index);
    Point& Array::GetElement(int index);
    Array& Array::operator = (const Array& source);    // Assignment operator.
    Point& Array::operator [] (int index); //[] operator can be used for both reading and writing elements. 		
    friend ostream& operator << (ostream& os, const Array& ar); // Global Methods
};
#endif // Array_H

// point.h
#ifndef Point_H
#define Point_H
#include <string>
#include <iostream>
using namespace std;
class Point
{
private:
    double m_x;                                // X coordinate for Point 
    double m_y;                                // Y coordinate
public:
    // Constructors
    Point();                            // Default constructor
	Point(double new_x, double new_y);  // Initialize with x and y value
	~Point();						// Destructor
	Point(const Point& point);      // Copy Constructor
	string Point::ToString(void) const;            // String representation of a point
    friend ostream& operator << (ostream& os, const Point& point);  
};
#endif // Point_H

//array.cpp
#include "array.h"
#include "point.h"
#include <cmath>
#include <sstream>
#include <iostream>
using namespace std;

Array::Array()                                     
{
 //   cout << "Array Default Constructor... done" << endl;
    m_size = 10;
    m_data = new Point[m_size];
}
Array::Array(const int new_size) //specified by the size input argument							
{
 //   cout << "Array size Constructor" << endl;
    m_size = new_size;
    m_data = new Point[m_size];
}
Array::Array(const Array& ar) 
{
 //   cout << "Array copy Constructor...done" << endl; 
    m_size    =    ar.m_size;  // shallow copy - this is not dynamic alloc 
    if (ar.m_data)  // if not zeros then there is a ref. - Deep copy needed
    {
        cout <<"im here on copy"<<endl;
        m_data = new Point[ar.m_size];
        for (int i = 0; i < ar.m_size; i++)
            m_data[i]    =    ar.m_data[i];
    }
    else
        m_data = 0;  //NULL
}
Array::~Array()                                 
{
   // cout << "Array Destructor... done" << endl;
    delete [] m_data;
}
Array& Array::operator = (const Array& source)
{
  //  cout << "Array Assignment operator... done" << endl; 
    if (this == &source)
        {
      //  cout << "Same Array " << endl;
        return *this;
        }
    delete [] m_data;
  //  cout <<"Deleted m_data array" << endl; 
    m_size = source.m_size;  // shallow copy - this is not dynamic alloc 
    if (source.m_data)  // if not zeros then there is a ref. - Deep copy needed
    {
      //  cout <<"im here"<<endl;
 
        m_data = new Point[source.m_size];  // create a new pointee.
 
        for (int i = 0; i < source.m_size; i++)
            m_data[i]    =    source.m_data[i]; //copy the points from/to array
    }
    else
        m_data = 0;  //NULL
    return *this;
}
int Array::Size() 
{
    return m_size;
} 

void Array::SetElement(const Point& pt, int index)
{
	try{
		if (index >= m_size){throw 11;} // When the index is out of bounds, ignore the “set”.
	}
	 catch (int x )
	 {
		cout << "ERROR" << x << " ignore the set, because index more then massive size"<< endl;
	 }
	m_data[index] = pt;
	cout << "Set Element " << pt  << endl;
}
Point& Array::GetElement(int index)
{
   // cout << "GetElement...done " << endl;
	try 
	{
		if (index > m_size || index < 0){ throw 1;} // по заданию надо ставить вроде как -1 но что то ошибка какая то когда минус один ставишь разобраться потом 
	} 
	catch (double x)
	{
	cout << "index incorrect, index too big or too small " << x << endl; 
	}  
	return m_data[index];
}
ostream& operator << (ostream& os, const Array& ar)
{
    os << "\nArray Size = " << ar.m_size << endl;
 
    for (int i = 0; i < ar.m_size; i++)
        os << "Array [" << i << "]= "<< ar.m_data[i] << endl;
 
    return os;
}
Point& Array::operator [] (int index)
{
  //  cout << "Array [] operator" << endl;
 
    if (index > this->m_size)
	{//When the index is out of bounds, return the first element. 
     //   cout << "i am hreeeee" << endl;
        return this->m_data[0];
	}
    return m_data[index];
}


//point.cpp
#include "point.h"
#include <sstream>
#include <cmath>
using namespace std;

ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}
Point::Point() : m_x(0), m_y(0) {}
Point::Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {}
Point::Point(const Point& point)
{
    m_x = point.m_x;
    m_y = point.m_y;
}
Point::~Point() {}
std::string Point::ToString(void) const  // create a string representation of a point
{
    std::ostringstream os;
    os << m_x << " , " << m_y;
    std::string double_string = os.str(); // create a string like: “Point(1.5, 3.9)”
    return "Point(" + double_string + ")";
}

//main.cpp
#include <iostream>
#include <sstream>
#include "array.h"
#include "point.h"
using namespace std;
int main()
{
	{
    Point* p1 = new Point(1,12);
    Point* p2 = new Point(-3,4);
	Point* p3 = new Point(-32,21);
//	Point* p4 = new Point(-32,2122);
		cout << " TEST OF ToString fuctionf working : "; p2->ToString();
		cout << endl; 
    Array arr1(3);
    arr1.SetElement(*p1,0);
    arr1.SetElement(*p2,1);
	arr1.SetElement(*p3,5);
		cout << arr1.GetElement(0) << endl << arr1.GetElement(1) << endl << arr1.GetElement(2) << endl << endl;
    Array arr2(3);
	arr1 = arr1;// assignment operator test
		cout << arr1.GetElement(0) << endl << arr1.GetElement(1) << endl << arr1.GetElement(2) << endl << endl;
    arr2 = arr1;// assignment operator test
		cout << arr2.GetElement(0) << endl << arr2.GetElement(1) << endl << arr2.GetElement(2) << endl << endl;
    Array arr3(arr2);// Copy constructor test
		cout << arr3.GetElement(0) << endl << arr3.GetElement(1) << endl << arr3.GetElement(2) << endl << endl;
		cout << endl; 
		cout << "now let's test [] overload operator" << endl;
	Array* my_Array3 = new Array(3);
	my_Array3->SetElement(*p2,0);
	my_Array3->SetElement(*p3,1);
		cout << "my_Array3 [] operator " << (*my_Array3)[1] << endl;
}
	system ("pause");
    return 0;
} 
1
2
3
4
5
6
7
8
9
10
11
12
void Array::SetElement(const Point& pt, int index)
{
	try{
		if (index >= m_size){throw 11;} // When the index is out of bounds, ignore the “set”.
	}
	 catch (int x )
	 {
		cout << "ERROR" << x << " ignore the set, because index more then massive size"<< endl;
	 }
	m_data[index] = pt; // you didn't ignore it.
	cout << "Set Element " << pt  << endl;
}


By the way, in main() ¿why do you create the points dynamically?
I don't remember may be it was wording of assignment))) How you think will be more optimize (without inheritance if only) ?

Now ) I'm realize that I'm compliantly forget about memory leak, so I add several lines in end of main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
			cout << "my_Array3 [] operator " << (*my_Array3)[1] << endl;

		delete p1;
		delete p2;
		delete p3;
		delete my_Array3;
		delete arr1;
		delete arr2;

	}
		system ("pause");
		
	}

but compiler don't allow me delete arr1, arr2 he said that "expression must have pointer or handle type " what does it mean ???
and also, I add a lines
1
2
3
4
5
6
7
8
~Array()                      // Destructor		
	{
		cout << "Array destructor... done " << endl;
	}
~Point()							// Destructor
	{
		cout << "Point destructor... done " << endl;
	}

too destructors (both classes) in order to control their works , but I didn't see that any destructor from Point or Array class done a work... this is confuse me... could you help me ?

There is other problem In very beggining of main file I try to test working ToString() fuction in line
 
cout << " TEST OF ToString fuctionf working : "; p2->ToString();


but compiler does give me nothing after string " TEST OF ToString fuctionf working : "
What should I done wrong ????
by the way , many sorry . I'm completely forget say MANY MANY THANKS FOR YOUR HELP!!!!

yep it was wording:
" Add a data member for a dynamic C array of Point objects (Point* m_data)."
Last edited on
You should not delete arr1 and arr2, they haven't been created with "new".
WHERE IS MY BRAIN !!! many thanks !!! ))

could you help me realize why I'm don't see cout from destructors, please !!!
and why my "TEST OF ToString fuctionf working : " doesn't work
cout << " TEST OF ToString fuctionf working : "; p2->ToString();
See nothing? You put a ";" in place of a "<<". For not seeing the cout from the destructors, i don't know :/
 
cout << " TEST OF ToString fuctionf working : " << p2->ToString() << endl;


without changes (((((
Try with this:

1
2
3
4
5
6
7
8
9
std::string Point::ToString(void) const  // create a string representation of a point
{
    std::ostringstream os;
    os << m_x << ", " << m_y;
    std::string double_string = "Point("; // create a string like: “Point(1.5, 3.9)”
    double_string += os.Str();
    double_string += ")";
    return double_string;
}


I hope you know where it goes ;)
no (((

still blank
Try like:
1
2
3
std::string ToStr = p2->ToString();
// Put a breakpoint here!
cout << " TEST OF ToString function working: " << ToStr << endl;


When having a breakpoint and debugging, make a watch to ToStr's value... Is it also blank? If it is, there's something wrong in the ToString function.
Topic archived. No new replies allowed.