accessing const array reference in inherited-templated class

Discussion in 'C++' started by hweekuan@yahoo.com, Dec 9, 2005.

  1. Guest

    Hi,

    I got an error with gnu C++ but not with intel C++ (icc). The distilled
    code is given below, a variable "T" is stored as a const T& in the base
    class and when the derived class try to access it, the compiler gives
    an error. Compile error with gnu C++ and version of C++ compiler is
    also given below. I don't know if its compiler bug.

    Any help and comments will be very much appreciated. Thank you.

    ----------------------------------------------------------------
    #include <iostream>
    #include <vector>

    template<class T> class baseClass {
    protected:
    baseClass(const T& r):p(r) { }
    const T& p;
    };

    template<class T> struct derivedClass : public baseClass<T> {

    derivedClass(const T& u) : baseClass<T>(u) { }

    void f() { std::cout<<baseClass<T>::p[0]<<std::endl; } // error here
    };

    typedef std::vector<double> V;

    int main() {

    V v(2,1);

    derivedClass<V> dc1(v);

    dc1.f();
    }
    ------------------------------------------
    // compile error
    inheritance.cc: In member function `void derivedClass<T>::f() [with T =
    V]':
    inheritance.cc:27: instantiated from here
    inheritance.cc:16: error: invalid types `const V&[int]' for array
    subscript

    // compiler specs
    Reading specs from /usr/libexec/gcc/darwin/ppc/3.3/specs
    Thread model: posix
    gcc version 3.3 20030304 (Apple Computer, Inc. build 1493)
    , Dec 9, 2005
    #1
    1. Advertising

  2. * :
    >
    > I got an error with gnu C++ but not with intel C++ (icc). The distilled
    > code is given below, a variable "T" is stored as a const T& in the base
    > class and when the derived class try to access it, the compiler gives
    > an error. Compile error with gnu C++ and version of C++ compiler is
    > also given below. I don't know if its compiler bug.
    >
    > Any help and comments will be very much appreciated. Thank you.


    I can't see anything technically wrong, and it compiles fine with MSVC 7.1,
    MinGW g++ 3.4.4, and especially, Comeau Online 4.3.3.

    Perhaps just update the compiler.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 9, 2005
    #2
    1. Advertising

  3. Guest

    thanks for the reply.
    i have not visited comp.lang.c++ for sometime, what is "Comeau Online
    4.3.3" ?
    , Dec 9, 2005
    #3
  4. * :
    > thanks for the reply.
    > i have not visited comp.lang.c++ for sometime, what is "Comeau Online
    > 4.3.3" ?


    Comeau is a compiler front-end that transforms your compiler into a
    standard-conforming one.

    Comeau Online is the online version, available on the web (just google),
    and is the nearest thing to an official "is it standard code" test.

    I guess Greg Comeau will correct me if this is not a 100% explanation...


    Cheers,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 9, 2005
    #4
  5. On 8 Dec 2005 18:44:34 -0800, wrote:

    > void f() { std::cout<<baseClass<T>::p[0]<<std::endl; } // error here


    Like the others said, it should work. Perhaps you could try
    parentheses as a workaround (don't know if it helps, though):

    void f() {
    std::cout << (baseClass<T>::p)[0] << std::endl;
    }

    You should include <ostream> as well as <iostream> if you are using
    std::endl ... most of the time, it seems to work without, but
    according to the standard it should be included as well.

    --
    Bob Hairgrove
    Bob Hairgrove, Dec 9, 2005
    #5
    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. Lionel B
    Replies:
    4
    Views:
    334
    Lionel B
    Apr 7, 2005
  2. =?ISO-8859-1?Q?Xavier_D=E9coret?=

    Inherited members from templated class

    =?ISO-8859-1?Q?Xavier_D=E9coret?=, Mar 7, 2006, in forum: C++
    Replies:
    4
    Views:
    288
    Victor Bazarov
    Mar 7, 2006
  3. Javier
    Replies:
    2
    Views:
    547
    James Kanze
    Sep 4, 2007
  4. chhenning
    Replies:
    5
    Views:
    354
    chhenning
    Feb 13, 2008
  5. 7stud --
    Replies:
    11
    Views:
    387
    7stud --
    Nov 9, 2007
Loading...

Share This Page