Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

cast overload

I created a class matrix, and a derived class vector.
I try to do a cast from matrix (class matriz) to vector (class vetor), but it doesn't work.
My cast:
operator vetor () const;

The output:
matriz.h:25: error: expected type-specifier before vetor

The vetor class is in vetor.h, and matriz is in matriz.h.

Thank you
[364 byte] By [abelsiqueira] at [2007-11-11 10:17:23]
# 1 Re: cast overload
You need to define the conversion operator, not just declare it. Also, did you put the conversion operator inside the Matriz class? Please show us more code, it will be much easier to find the problem.
Danny at 2007-11-11 20:59:13 >
# 2 Re: cast overload
I've defined the cast and the declaration is in matriz:

matriz.h
...
class matriz {
public:
matriz (int , int , int = 0); //type => 0-Nula, 1-Identidade, -1-randomica
matriz (const matriz &, int = 0);
~matriz ();
friend ostream &operator<< (ostream &, const matriz &);
friend matriz operator* (float x, const matriz & A);
float& operator() (int , int );
float operator() (int , int ) const;
matriz operator* (matriz const&) const;
matriz operator* (float x) const;
matriz operator+ (matriz const&) const;
matriz operator- (matriz const&) const;
matriz operator- () const;
void operator= (matriz const&);
matriz operator!() const;
// operator vetor () const;
int get_lin () { return n_lin; }
int get_col () { return n_col; }
protected:
int n_lin, n_col;
float *val;
};
...
matriz::operator vetor() const {
if (n_col != 1) {
printf("Matriz no vetor\n");
throw("Matriz no vetor\n");
}
vetor temp(n_lin);
for (int i = 1; i <= n_lin; i++) temp(i) = (*this)(i);
return temp;
}
...

vetor.h:
...
#include "matriz.h"

class vetor : public matriz {
public:
vetor (int , int = 0); //Inicializa -1 - rand, 0 - nulo, x - 1 na pos x
vetor (const vetor &, int = 0);
float& operator() (int);
float operator() (int) const;
};

vetor::vetor (int n, int type) : matriz(max2i(n, 1), 1, min2i(0, type) )
{
if ((type > 0) && (type <= n)) val[type - 1] = 1;
}

vetor::vetor (const vetor & v, int copy) : matriz(v,1) { }

float& vetor::operator() (int x) {
return (val[x - 1]);
}

float vetor::operator() (int x) const{
return (val[x - 1]);
}
...

I'm beginning to think that the problem is that vetor is a derived class, for I have tried a cast with a float and it worked.

Thanks.
abelsiqueira at 2007-11-11 21:00:13 >
# 3 Re: cast overload
I think you should try the following organization of your files:
// matriz.h

class vetor;

class matriz
{
. . .
operator vetor() const;
}

// matriz.cpp

#include vetor.h

. . .

matriz::operator vetor() const
{
. . .
}

// vetor.h

#include matriz.h

class vetor : public matriz
{
. . .
};
I hope this helps.
Viorel at 2007-11-11 21:01:06 >
# 4 Re: cast overload
I think your code has sevreal problems. First,
void operator= (matriz const&);

This isn't the way to define the assignment operator. The return value must be matriz&, not void. Secondly, the parameter should normally be const qualified (it's not a syntax error, but it's a design bug probably).
Also, instead of defining operator! you should simply define a Boolean conversion operator. This will save you the bother of defining ==, !=, ! etc. separately.
Also, the binary operators +, - * etc, should be extern friend functions, not members and they should take two const matriz& parameters.
Finally, you can't declare operator vetor if you haven't defined vetor at this point yet. Apparently, you need to #include "vetor.h" before the definition of class matriz but because vector is derived from matriz, you run into circular dependencies, which means that the design is flawed.
A vector should be an independent class, not a class derived from matriz. When you make vetor an independent class (i.e., without base classes), you will be able to #include "vector.h" before matriz and this will solve your peoblem.
Danny at 2007-11-11 21:02:12 >
# 5 Re: cast overload
Hey Viorel, that worked, I didn't realized that I could declare class vetor, and define it later, also defining only in vetor.h was fundamental too. Thank you very much.
Hi Danny, the = operator is void because the matrix that receive the information is the one that calls the operator, (*this), so it doesnt return anything. I could make it return a matrix like you said, that way I could do multiple assignments (A = B = C), thanks for the tip.
The ! operator is for the transpose matrix.
Thanks for your time.
abelsiqueira at 2007-11-11 21:03:16 >