Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return class from that class' member function?

Status
Not open for further replies.

forlorn

Programmer
Dec 12, 2001
28
TR
Hi,
i'm trying to write a simple matrix class. I want to a add a function that transpozes the matrix and returns the transpozed one.

CMatrix CMatrix::Transpose()
{
CMatrix mat;
int temp;

temp = m_ROW;
m_ROW = m_COL;
m_COL = temp;

(A) mat = new CMatrix( m_ROW, m_COL );

// etc.

return mat;
}

Compiler tells me that "operator =" function is unavailable.( For line labeled with (A) )

Do i have to define functions for all operators?

And also what is a "copy constructor"? VC++ complains about lacking one of them.

By the way, if you happen to know about a simple matrix class, would you please tell me where to find it.

Thanks a lot.


 
A question to lead you to the answer:

When you allocate an object, do you assign it to an object... or to a pointer? Once you've answered that, then also think about what data type you want to return from the member function.
 
Right:
CMatrix CMatrix::Transpose()
{
CMatrix *mat;
int temp;

temp = m_ROW;
m_ROW = m_COL;
m_COL = temp;

(A) mat = new CMatrix( m_ROW, m_COL );

// etc.

return *mat;
}
or so:
CMatrix *CMatrix::Transpose()
{
CMatrix *mat;
int temp;

temp = m_ROW;
m_ROW = m_COL;
m_COL = temp;

(A) mat = new CMatrix( m_ROW, m_COL );

// etc.

return mat;
}
Do not forget to use delete after new!
To define operator "=", You can define copy constructor
CMatrix::CMatrix(CMatrix matr)
{
...
}
 
Thanks to both of you. I think jumping directly into C++ from Java without fully understanding pointers was a mistake. But it's getting better by your help.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top