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
# 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.
# 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.