473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

accessing const array reference in inherited-templated class

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
4 1573
* 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
thanks for the reply.
i have not visited comp.lang.c++ for sometime, what is "Comeau Online
4.3.3" ?

Dec 9 '05 #3
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
9
by: JKop | last post by:
Let's say you have a global const variable for the name of your application. Which do you think is preferrable?: A) char const g_application_name = "ChocolateCheese"; B) const char* const...
6
by: Dylan Nicholson | last post by:
Is there any way of declaring the parameter "array" below to be const, so that the function as written will not compile: void foo(int array /*const*/) { array = array; // should not be allowed...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
2
by: vishwesha.guttal | last post by:
Hi, I am having troble declaring a const array. If the array size is small, then one can do as follows: const double array = {1, 2, 3, 4, 5}; What if I have an array of size say 1000 or...
2
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.