By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

accessing const array reference in inherited-templated class

P: n/a
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 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* hw******@yahoo.com:

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?
Dec 9 '05 #2

P: n/a
thanks for the reply.
i have not visited comp.lang.c++ for sometime, what is "Comeau Online
4.3.3" ?

Dec 9 '05 #3

P: n/a
* hw******@yahoo.com:
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?
Dec 9 '05 #4

P: n/a
On 8 Dec 2005 18:44:34 -0800, hw******@yahoo.com 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
No**********@Home.com
Dec 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.