473,394 Members | 1,679 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,394 software developers and data experts.

Correct use of templated class'es inner structs/classes?

Hi

I'm currently trying to make my old code to compile with
new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not
sure if this is compiler bug or not.
Code very close to a example below fails to compile
because gcc 3.4.0 complains:
"error: expected a type got 'Y<T>::particle'"
at Y<T>::func()'s iterator declaration.
EXAMPLE
(I actually haven't tried to get this compile).
-----------------------------------

example.h:
template <typename T>
class Y{
public:

struct particle {
OtherClass<T> data;
};

void func();

std::vector<Y<T>::particle> list;
}
example.cpp:
template <typename T>
void Y<T>::func(){
typename std::vector<Y<T>::particle>::iterator i;

i = list.begin();

while(i != list.end()){
// do something
}
}
--------------------------

TIA,
Tomas Ukkonen

Jul 22 '05 #1
4 1510
Tomas Ukkonen wrote in news:pa****************************@iki.fi in
comp.lang.c++:
Hi

I'm currently trying to make my old code to compile with
new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not
sure if this is compiler bug or not.
Code very close to a example below fails to compile
because gcc 3.4.0 complains:
"error: expected a type got 'Y<T>::particle'"
at Y<T>::func()'s iterator declaration.
EXAMPLE
(I actually haven't tried to get this compile).
Why not ?.
-----------------------------------

example.h:
template <typename T>
class Y{
public:

struct particle {
OtherClass<T> data;
T data; // to make a compilable example !
};

void func();

std::vector<Y<T>::particle> list;
std::vector< typename Y<T>::particle > list;

However Y (without the <T>) is an injected class name so you should
be able to do this:

std::vector< Y::particle > list;

However of the compilers I tried on CBuilderX previeiw (EDG based)
handled it, g++ 3.4 (prereleas) didn't, your version maybe later than
mine so try it.

}
missing ;


example.cpp:
This should be in a header file.
template <typename T>
void Y<T>::func(){
typename std::vector< Y<T>::particle >::iterator i;
typename std::vector< typename Y<T>::particle >::iterator i;


i = list.begin();

while(i != list.end()){
// do something
}
}


missing ;
The typename is required as Y<T> is dependant on a template argument
so particle in Y<T>::particle is a dependant-name, you need to tell
the compiler its a type, otherwise it assumes its a non-type.

g++ 3.3.x didn't require this as it didn't support 2-phase name
lookup, as its supposed to. i.e. 3.4 is correct.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Tomas Ukkonen <to***********@iki.fi> wrote in message news:<pa****************************@iki.fi>...
Hi Code very close to a example below fails to compile
because gcc 3.4.0 complains:
"error: expected a type got 'Y<T>::particle'"
at Y<T>::func()'s iterator declaration.


Indeed. Without knowing T, gcc cannot tell whether particle is
a member type, a data member, a member function, or a member template.

The solution is (obviously) to tell the compiler:

std::vector<typename Y<T>::particle> list;

Regards,
Michiel Salters
Jul 22 '05 #3
On Fri, 07 May 2004 07:04:30 -0700, Michiel Salters wrote:
Tomas Ukkonen <to***********@iki.fi> wrote in message
news:<pa****************************@iki.fi>...
Code very close to a example below fails to compile
because gcc 3.4.0 complains:
"error: expected a type got 'Y<T>::particle'"
at Y<T>::func()'s iterator declaration.


Indeed. Without knowing T, gcc cannot tell whether particle is
a member type, a data member, a member function, or a member template.

The solution is (obviously) to tell the compiler:

std::vector<typename Y<T>::particle> list;


Thanks. I managed to get gcc to accept something similar
while waiting for reply. I have never really read how
templates should work according to standard so I wasn't
sure what the correct behaviour of gcc should be.

--
Tomas Ukkonen
Jul 22 '05 #4
On Fri, 07 May 2004 11:07:49 +0000, Rob Williscroft wrote:
Tomas Ukkonen wrote in news:pa****************************@iki.fi in
comp.lang.c++:
EXAMPLE
(I actually haven't tried to get this compile). Why not ?.


Guess I'm lazy. It was just pseudocode for
illustrating the problem.
std::vector<Y<T>::particle> list;

std::vector< typename Y<T>::particle > list;

However Y (without the <T>) is an injected class name so you should
be able to do this:

std::vector< Y::particle > list;

However of the compilers I tried on CBuilderX previeiw (EDG based)
handled it, g++ 3.4 (prereleas) didn't, your version maybe later than
mine so try it.

Thanks for help. It seems I started asking for help a bit too
quickly as I managed to get the code working myself. I don't have
time to test the latter solution right now.
example.cpp:

<EXAMPLE CODE> This should be in a header file.


Yes, but I usually code in a style where I either put templated code to
cpp files as normally and then include it to header file or
use explicit template instantations.

--
Tomas Ukkonen
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Foster | last post by:
Hi, I'm having some difficulty using types which are defined in a base class inside a derived class. The problem crops up using template classes. The following test code encapsulates what I'd...
0
by: Chris Jefferson | last post by:
Now, C++ doesn't actually provide true variable-length templated classes, for example you can't do (as I found recently!) template<class T> class tuple { ... } template<class T, class U>...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
7
by: Paulustrious | last post by:
How do I extend a template class for a specific template data type? I am trying to achieve something like.... public class SomeItem{} public class SomeProcess < T> { } and now the extension ...
1
by: ramunasg | last post by:
Hello, g++ refuses to compile the following code, by saying that "pt" is not in the scope of "Second" class (when I add scope by changing "pt = 0" to "First<T>::pt = 0" g++ comples clenely), but...
4
by: jschon | last post by:
Hi, I'm trying to write a thread safe hash map class. Basically I have put some packaging around the STL map class to ensure thread safe access. The container is a templated class. I'm having...
12
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to...
1
by: cpunerd | last post by:
Hello, I'm not new to C++, but for some reason, until now I'd never had a need for deriving templated classes. Now though, I find myself seeing a weird problem. If I have a templated base class...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.