class function returning another class

Hi all,

I have 2 classes, vector and matrix defined in separate header files. In the vector class I have a function performing "outer-product" of two vectors which results in a matrix type.
In the vector file, the header for the matrix is also included.

Using such codes I tried compiling and everything seemed to be alright but I got runtime error which I could not figure out why.
I checked and it seems like the operations have been performed correctly in the vector class and the whole problem to me looks like there is problem with "returning" a "matrix type" by a function of class "vector" and not the computations therein.

Any ideas what the correct way for class function returning another class is?

many thanks,
Shervin
Last edited on
You return an instance of a class just like you'd return anything else.

It sounds like a copy constructor problem. Post the header for the matrix class.
I use pointer functions, and return pointers into a list (with an iterator) of pointers every time I want to pass a non-basic member. I consider non-basic members to be int, double, float, char, etc.

So...something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Matrix * newMatrix(....){
    ....
    ....
    matrix *myMatrix = new Matrix(....);
    return myMatrix;
} //This function creates a new matrix and passes the pointer to the matrix back to the creating function.

void menu(){
    list<Matrix*> matrices;
    list<Matrix*>::iterator it;
    Matrix *temp;
    ....
    ....
    temp = newMatrix(....);
    matrices.push_back(temp);
    ....
    ....
}
thanks guyz, yes it was actually a copy constructor problem!
Topic archived. No new replies allowed.