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:

FixedSizeMatrix y(5,5);
cout<<y;
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?

Recommended Answers

All 4 Replies

Show us your code.

Method operator<< should be,

friend ostream &operator<<(ostream &out,const FixedSizeMatrix &ref){
   .....        
  return out;
}
inline ostream& perator<<(ostream& os, FixedSizeMatrix obj)
{

}

Sorry bout that...

inline ostream& operator << (ostream& os, FixedSizeMatrix obj)
{
   obj.print(os);
   return os;
}

Try this. And inline won't do anything helpful by the way.

ostream& operator << (ostream& os, FixedSizeMatrix& obj)
{
   obj.print(os);
   return os;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.