C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old February 26th, 2003, 11:33 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #17  
Old February 26th, 2003, 11:36 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,595 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 32 m 17 sec
Reputation Power: 1009
>> 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.

Reply With Quote
  #18  
Old February 27th, 2003, 01:02 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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?

Reply With Quote
  #19  
Old February 27th, 2003, 12:01 PM
maskzilla maskzilla is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 11 maskzilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #20  
Old February 27th, 2003, 02:08 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"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.

Reply With Quote
  #21  
Old February 27th, 2003, 05:45 PM
maskzilla maskzilla is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 11 maskzilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #22  
Old February 27th, 2003, 06:44 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,595 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 32 m 17 sec
Reputation Power: 1009
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.

Reply With Quote
  #23  
Old February 27th, 2003, 10:49 PM
maskzilla maskzilla is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 11 maskzilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thank all of you for helping me with this. i finally got it all set up and its now working perfectly!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Debug assertion error ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT