accessing const array reference in inherited-templated class

H

hweekuan

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)
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
H

hweekuan

thanks for the reply.
i have not visited comp.lang.c++ for sometime, what is "Comeau Online
4.3.3" ?
 
A

Alf P. Steinbach

* (e-mail address removed):
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
 
B

Bob Hairgrove

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.
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top