Inheriting operator() and templates

D

Drew McCormack

Does operator() get inherited? I would have thought so, but I can't get
the following to work:

I have a base class that takes its derived class as a template template
parameter (the Barton-Nackmann trick). It has a operator() defined.


template <typename NumType, template <typename T> class DerivedType>
class Matrix {
public:
NumType operator()( const int row, const int col ) const;
NumType& operator()( const int row, const int col );
}


I have a derived class which does not redefine the operator, the
intention being to inherit it.


template <typename NumType>
class GeneralMatrix : public Matrix<NumType, GeneralMatrix> {
}


If I try to use this derived class, like this

GeneralMatrix<double> matrix(3,3); // Constructor
matrix = 4.0; // Initialize
matrix(1,1) = 2.0;


I get the following error with gcc 3.3:

error: no `NumType GeneralMatrix<NumType>::eek:perator()(int, int) const'
member function declared in class `GeneralMatrix<NumType>'


Can you inherit operator()? Why would this not work for me?


Drew McCormack
 
V

Victor Bazarov

Drew said:
Does operator() get inherited?

Yes, it does.
I would have thought so, but I can't get
the following to work:

I have a base class that takes its derived class as a template template
parameter (the Barton-Nackmann trick). It has a operator() defined.


template <typename NumType, template <typename T> class DerivedType>
class Matrix {
public:
NumType operator()( const int row, const int col ) const;
NumType& operator()( const int row, const int col );
}


I have a derived class which does not redefine the operator, the
intention being to inherit it.


template <typename NumType>
class GeneralMatrix : public Matrix<NumType, GeneralMatrix> {
}


If I try to use this derived class, like this

GeneralMatrix<double> matrix(3,3); // Constructor
matrix = 4.0; // Initialize
matrix(1,1) = 2.0;


I get the following error with gcc 3.3:

error: no `NumType GeneralMatrix<NumType>::eek:perator()(int, int) const'
member function declared in class `GeneralMatrix<NumType>'


Can you inherit operator()? Why would this not work for me?


Drew McCormack
--------------------------------------- This:
template <typename NumType, template <typename T> class DerivedType>
class Matrix {
public:
NumType operator()( const int row, const int col ) const;
NumType& operator()( const int row, const int col );
};


template <typename NumType>
class GeneralMatrix : public Matrix<NumType, GeneralMatrix> {
public:
GeneralMatrix(int,int) {}
};

int main()
{
GeneralMatrix<double> matrix(3,3); // Constructor
matrix(1,1) = 2.0;
}
----------------------------------------
compiles fine with Comeau online trial. Doesn't compile with VC++,
though, but not because of operator(). It does not want to accept
'GeneralMatrix' as the second template argument to 'Matrix' in the
'GeneralMatrix' definition.

Victor
 
S

Sharad Kala

Victor Bazarov said:
----------------------------------------
compiles fine with Comeau online trial. Doesn't compile with VC++,
though, but not because of operator(). It does not want to accept
'GeneralMatrix' as the second template argument to 'Matrix' in the
'GeneralMatrix' definition.

Perhaps VC++ is getting it wrong.
It compiles with VC++ if you change GeneralMatrix to -

template <typename NumType>
class GeneralMatrix : public Matrix<NumType, ::GeneralMatrix> {
public:
GeneralMatrix(int,int) {}
};

This is the usual injected-name issue, but I believe that the name should
have only been injected in the template body.

-Sharad
 
D

Denis Remezov

Drew said:
[...]

template <typename NumType, template <typename T> class DerivedType>
class Matrix {
public:
NumType operator()( const int row, const int col ) const;
NumType& operator()( const int row, const int col );
}
[...]

template <typename NumType>
class GeneralMatrix : public Matrix<NumType, GeneralMatrix> {
}
[...]

GeneralMatrix<double> matrix(3,3); // Constructor
matrix = 4.0; // Initialize
matrix(1,1) = 2.0;

I get the following error with gcc 3.3:

error: no `NumType GeneralMatrix<NumType>::eek:perator()(int, int) const'
member function declared in class `GeneralMatrix<NumType>'
[...]

Perhaps you were looking at something different from what you have
typed, because the error message seems irrelevant in this context.

Corrected for the missing ';', constructor and initialiser, the
above does compile with gcc 3.3.3 and even 2.95.3.

Denis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top