C++ destructor problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 22
Reputation: Peppercat101 is an unknown quantity at this point 
Solved Threads: 0
Peppercat101 Peppercat101 is offline Offline
Newbie Poster

C++ destructor problem

 
0
  #1
Oct 4th, 2009
Hi, Im new to c++ and Im struggling with something.

I have a load of classes that all connect to each other. I globally overloaded the << operator for each class. So in my main I do the following:

  1. FixedSizeMatrix y(5,5);
  2. cout<<y;
  3. cout<<y;

The first cout works fine but the second ne gives me a segementation fault. Now I know a segmentation fault is a problem with memory and the cnstructors and destructors. I put a cout statement in my destructor and to my surprise I saw that directly after the first cout<<y; statement is executed the destructor is called.

Im completely lost as to why this is. Any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,621
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: C++ destructor problem

 
0
  #2
Oct 4th, 2009
Show us your code.

Method operator<< should be,
  1. friend ostream &operator<<(ostream &out,const FixedSizeMatrix &ref){
  2. .....
  3. return out;
  4. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 22
Reputation: Peppercat101 is an unknown quantity at this point 
Solved Threads: 0
Peppercat101 Peppercat101 is offline Offline
Newbie Poster

Re: C++ destructor problem

 
0
  #3
Oct 4th, 2009
  1. inline ostream& perator<<(ostream& os, FixedSizeMatrix obj)
  2. {
  3.  
  4. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 22
Reputation: Peppercat101 is an unknown quantity at this point 
Solved Threads: 0
Peppercat101 Peppercat101 is offline Offline
Newbie Poster

Re: C++ destructor problem

 
0
  #4
Oct 4th, 2009
Sorry bout that...

  1. inline ostream& operator << (ostream& os, FixedSizeMatrix obj)
  2. {
  3. obj.print(os);
  4. return os;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,242
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 154
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: C++ destructor problem

 
1
  #5
Oct 4th, 2009
Try this. And inline won't do anything helpful by the way.

  1. ostream& operator << (ostream& os, FixedSizeMatrix& obj)
  2. {
  3. obj.print(os);
  4. return os;
  5. }
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC