I've created two classes, one to deal with vectors and the other to deal with tensors. I want to have a function in the vector class returning a tensor and a function in the tensor class returning a vector. The classes implementation is somewhat like the following: ( The operation performed by the ~ operator is T_a,b = Aa . Bb, where A and B are vectors and T_a,b is a tensor. The operation performed by the * operator is a multiplication of a tensor by a vector, returning a vector )
1 2 3 4 5 6 7 8 9 10 11 12 13
template <typename T>
class vector{
//...
tensor<T> operator ~ ( vector<T> X ); //(1)
//...
};
//...
template <typename T>
class tensor{
//...
vector<T> operator * ( vector<T> X );
//...
};
The both definitions are in the same file. I am having problems compiling the code. Apparently, the compiler doesn't reconize the argument //(1) as an instance of the tensor class, 'cause it appears before the class is defined... I just can't figure a solution out. I'd appreciate your comments.
Thanks!
You need to prototype the class before you define it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
template <typename T> class tensor;
template <typename T>
class vector
{
public:
tensor <T> invert() const; // or whatever it is that you are trying to do to your vector
};
template <typename T>
class tensor
{
public:
vector <T> operator * ( const vector <T> v ) const;
};
The ~ operator is a unary function, and it should always return the same type of object you started with. This is called the principle of least surprise.
Likewise, mathematical operations take const objects (and return a new object as you want to do) -- there is no reason to modify the RHS terms.