Inheriting operator() and templates

Discussion in 'C++' started by Drew McCormack, Jul 12, 2004.

  1. 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
     
    Drew McCormack, Jul 12, 2004
    #1
    1. Advertising

  2. Drew McCormack wrote:
    > 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
     
    Victor Bazarov, Jul 12, 2004
    #2
    1. Advertising

  3. Drew McCormack

    Sharad Kala Guest

    "Victor Bazarov" <> wrote in message
    news:%swIc.2050$...
    > ----------------------------------------
    > 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
     
    Sharad Kala, Jul 12, 2004
    #3
  4. Drew McCormack wrote:
    >

    [...]
    >
    > 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
     
    Denis Remezov, Jul 12, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews

    Inheriting operator []

    Thomas Matthews, Mar 1, 2004, in forum: C++
    Replies:
    4
    Views:
    361
    Thomas Matthews
    Mar 2, 2004
  2. JKop
    Replies:
    3
    Views:
    506
  3. Generic Usenet Account
    Replies:
    3
    Views:
    846
    Generic Usenet Account
    Jul 14, 2005
  4. recover
    Replies:
    2
    Views:
    845
    recover
    Jul 25, 2006
  5. johnmmcparland

    inheriting the operator<<

    johnmmcparland, Mar 22, 2007, in forum: C++
    Replies:
    6
    Views:
    413
Loading...

Share This Page