views:

84

answers:

2

I'm having trouble compiling the following header. It's my first experience with templates and I guess I'm getting something wrong. The compilers point errors at vector<vector<T>> data_; and the operator overloading function. I would like the data_ vector to have the same type as the OptBaseMatrix object but I'm not sure how to do it... I really don't know how to solve this problem. Help!

#ifndef OPTBASEMATRIX_H
#define OPTBASEMATRIX_H

#include <vector>

template<typename T>
class OptBaseMatrix 
{ 
public:
 vector<vector<T>> data_; 

 OptBaseMatrix(int rows, int cols);
 ~OptBaseMatrix();

 void readMatrix();
 void printMatrix();
 int getRows();
 int getCols();

    OptBaseMatrix<T> operator+(const OptBaseMatrix<T>& matrix1, const OptBaseMatrix<T>& matrix2);

private:
 int rows_; 
 int cols_; 
};

#endif // OPTBASEMATRIX_H

UPDATE: Here is a snippet from the debugger log:

Error   1   error C2143: syntax error : missing ';' before '<'  optbasematrix.h 17  TD2
Error   2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   optbasematrix.h 17  TD2

I've tried modifying vector> data_; by vector > data_; and still get the same error :/ I read somewhere that my template class header (.h) and implementation (.cpp) must be in the same file... is this possibly related ?

UPDATE 2: Wow ! I had forgotten "using namespace std;". The problem seems fixed now !

+2  A: 

Try:

vector<vector<T> > data_; 
Daniel Daranas
It may perhaps be worth noting that Bjarne Stroustrup calls this - i.e. that one cannot say vector<vector<double>> v but has to say vector< vector<double> > v - one of C++'s "embarrassments". See, for example, http://www2.research.att.com/~bs/DnE2005.pdf It seems that, with C++1y on the horizon, this problem won't be with us for much longer.
Alexandros Gezerlis
+8  A: 

You need to put a space between the two >.

 vector<vector<T> > data_;

Without the space, >> is treated as a stream-extraction/right-shift operator.

Additionally, you either need to declare operator+ as a free function or you must declare it with one parameter only:

// Member function
Matrix<T> operator+(const Matrix<T>& other) const;

// Free function (`friend` makes the function free
// even though it's declared within the scope of the class definition)
friend Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs);
avakar
This is the kind of thing that takes hours or days to figure out the first time, but then you never forget!
John Dibling
'friend' has more to it than just allowing you to declare a free function from within the class curly braces. If the function is defined outside of the class braces then it will allow it access to the private members of the class. If the function is also defined inside the class braces then it will have the added effect of hiding it from the lookup rules of additions where the parameters are not of the templated class, which has its own advantages... BTW: it is usually recomended to implement operator+= (must be member) and then a free function operator+ that calls on +=
David Rodríguez - dribeas
Thanks! However, I still get errors (see update).
Olivier Lalonde