|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#16
|
|||
|
|||
|
Scorpions4ever,
Print Scrn alone works for me--I get a screen shot of the active window. M1.reset(); I looked in the MSDN library for reset() and I looked in the appendix of my book and I couldn't find it. I am pretty frustrated with C++ functions because I have no way to determine if one exists, or when I do have the name, I can't find any information on the parameters. Is there a reference that lists functions by categories somewhere that I can get? For instance in PHP, my book had an appendix that listed every function arranged by general categories. |
|
#17
|
||||
|
||||
|
>> I looked in the MSDN library for reset() and I looked in the appendix of my book and I couldn't find it.
Hehe, you're looking at the wrong places . You should be looking at maskzilla's matrix.cpp code. reset() is a member function of the matrix class and initializes all the elements to 0 (if he hasn't changed it since I last saw it, that is) ![]() [edit] changed the word "method" to member function above. Egad! I've been working too much with Delphi![/edit] Last edited by Scorpions4ever : February 26th, 2003 at 11:38 PM. |
|
#18
|
|||
|
|||
|
Ok, I won't look for reset() anymore, but is there a list of C++ functions anywhere, or are there no functions that are part of the standard?
|
|
#19
|
|||
|
|||
|
haha thank you guys so much for helping me.. now the onnnnly thing i have left are the friend functions, i have re-uploaded the files that are correct up to that point, my question is, are my friend functions initialized and defined correctly? and im getting that error for debug assertion when the main program gets to calling the friend functions
you guys rock for helping me soooo much! |
|
#20
|
|||
|
|||
|
"and im getting that error for debug assertion when the main program gets to calling the friend functions"
Did you check to see if it was the same problem? Your array index is probably going out of bounds again. In the friend function, use cout to display the index value before using it to see if it's inbounds. |
|
#21
|
|||
|
|||
|
Code:
matrix operator -(matrix& M1, matrix& M2)
{
if((M1.rows != M2.rows)||(M1.columns!=M2.columns))
{ exit(1);}
matrix x(M1.rows, M2.columns);
x.reset();
int size = M1.rows * M2.columns;
for(int i = 0; i < size; i++)
{
cout << i << endl;
x(i) = M1.m[i] - M2.m[i];
}
cout << "RIGHT BEFORE RETURN X" << endl;
return x;
}
okay, this is just the one friend im trying to get work.. the print out when i run it is " 0 1 2 3 4 5 6 7 8 RIGHT BEFORE RETURN X " then it terminates... so is it having the problem when it is trying to return it to the value the call in the main.cpp is M1 = (M1 - M2); im pulling my hair out ![]() |
|
#22
|
||||
|
||||
|
I have a feeling this is because you haven't provided a copy constructor for your class. WHENEVER a class dynamically allocates and destroys memory, you have to be careful to create a copy constructor of your own something like this:
Code:
in matrix.h
public:
matrix::matrix(int r, int c);
matrix::matrix(const matrix &); // <<--- Copy constructor
in matrix.cpp:
matrix::matrix(const matrix &mat) {
// Delete old memory if necessary
if (m)
delete [] m;
rows = mat.rows;
columns = mat.columns;
m = new double[rows * columns];
for (int i = 0; i < (rows * columns); i++)
m[i] = mat.m[i];
}
Now, as to why you should create a copy constructor for classes that dynamically allocate memory. If you don't create one, then the compiler will create a default one for you. All the default copy constructor does is copy the data elements from one class to another. So let's analyze how this will break your program if no copy constructor is specified: Code:
matrix operator -(matrix& M1, matrix& M2)
{
// snip
matrix x(M1.rows, M2.columns);
// some more snips
cout << "RIGHT BEFORE RETURN X" << endl;
return x;
}
In your main function:
M1 = M1 - M2;
Now the matrix x is the key. Since you didn't create a copy constructor, the default one will just simply copy all the elements over. This means that after it runs M1 = M1 - M2, M1.m is pointing to the same spot as x.m is pointing to. Are you following me so far??. Now x is a variable local to the operator - function. So after the function exits, the destructor for it is called. So guess what? x frees the memory it dynamically allocated. Now M1.m is pointing to the same memory area, which was just freed by the destructor. Presto! instant problem. So how do we cure this problem. Simple -- we declare a copy constructor of our own. Within the copy constructor, we allocate new memory and copy the data from the source to our newly allocated memory. Actually I was meaning to demonstrate a variation of this bug as a puzzle of the month for this forum .BTW you have some more problems in your code. For one thing your = operator ought to be declared with return value matrix & rather than void. Otherwise you can't chain M1 = M2 = M3 and so on. See http://www.devhood.com/tutorials/tu...tutorial_id=502 for more. Last edited by Scorpions4ever : February 27th, 2003 at 06:47 PM. |
|
#23
|
|||
|
|||
|
thank all of you for helping me with this. i finally got it all set up and its now working perfectly!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Debug assertion error ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|