473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template problem with g++

Fab
All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------
template<class T>
class myvector { // Just a simplification of std::vector
class iterator {
T a;
iterator &operator++();
};

iterator &begin();
iterator &end();
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo {

int abc() {
myvector<mytype<T foo;
// DOES work:
//for (myvector<mytype<int::iterator iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytype<T::iterator iter = foo.begin(); iter !
= foo.end(); ++iter) {
// ...
}
return 0;
}
};
---------------------------------------------------------------------

In method Foo::abc if I specify int as template argument for mytype,
everything works, but if I want to use T (the template argument of Foo
class), the compiler reports this error:

foo.cpp: In member function 'int Foo<T>::abc()':
foo.cpp:28: error: expected `;' before 'iter'
foo.cpp:28: error: 'iter' was not declared in this scope

I haven't personally testes, but apparently the Microsoft compiler
(Visual Studio 2005) accept that code.

Any help is appreciated!
Thanks,
Fabrizio

Mar 2 '07 #1
8 1865
Fab wrote:
All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------
template<class T>
class myvector { // Just a simplification of std::vector
class iterator {
T a;
iterator &operator++();
};

iterator &begin();
iterator &end();
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo {

int abc() {
myvector<mytype<T foo;
// DOES work:
//for (myvector<mytype<int::iterator iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytype<T::iterator iter = foo.begin(); iter !
= foo.end(); ++iter) {
for( typename myvector<mytype<T::iterator...
// ...
}
return 0;
}
};
Mar 2 '07 #2
Fab
for( typename myvector<mytype<T::iterator...
A-AH! Gotcha!
Thanks Mark!

Fab

Mar 2 '07 #3
Fab wrote:
All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------
template<class T>
class myvector { // Just a simplification of std::vector
class iterator {
T a;
iterator &operator++();
};

iterator &begin();
iterator &end();
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo {

int abc() {
myvector<mytype<T foo;
// DOES work:
//for (myvector<mytype<int::iterator iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytype<T::iterator iter = foo.begin(); iter !
= foo.end(); ++iter) {
// ...
}
return 0;
}
};
---------------------------------------------------------------------

In method Foo::abc if I specify int as template argument for mytype,
everything works, but if I want to use T (the template argument of Foo
class), the compiler reports this error:

foo.cpp: In member function 'int Foo<T>::abc()':
foo.cpp:28: error: expected `;' before 'iter'
foo.cpp:28: error: 'iter' was not declared in this scope

I haven't personally testes, but apparently the Microsoft compiler
(Visual Studio 2005) accept that code.

Any help is appreciated!
Thanks,
Fabrizio
Whew! Now this will work. The main problem was the typename
keyword was required. To get the program to be errorless was
a another story :)

HTH

-----------------------------------------------------------
template<class T>
class myvector
{ // Just a simplification of std::vector
public:
class iterator
{
public:
T a;
bool operator!=( const iterator &b ) { return true; }
iterator & operator++() { return *this; }
iterator operator++( int ) { return *this; }
};

iterator begin() { return iterator(); }
iterator end() { return iterator(); }
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo
{
public:
void abc()
{
myvector<mytype<T foo;
// DOES work:
//for (myvector<mytype<int::iterator iter = foo.begin();
//iter != foo.end(); ++iter) {

// DOES NOT work:
for (typename myvector<mytype<T::iterator iter = foo.begin();
iter != foo.end(); ++iter)
{

}

}
};

int
main()
{
Foo<inttest;
test.abc();
return 0;
}
Mar 2 '07 #4
Fab
Piyo,
Thanks for your reply as well. The program I sent was just to describe
my problem.
In my application at the end I use std::vector, but in my test I
wanted to rule out any possible problem related to how STL are
implemented in my distribution (in particular, I wanted to focus on
compiler behavior).

I can't say that today I didn't learn anything...

Thanks again!
Fab
On Mar 1, 4:18 pm, Piyo <cybermax...@yahoo.comwrote:
Fab wrote:
All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------
template<class T>
class myvector { // Just a simplification of std::vector
class iterator {
T a;
iterator &operator++();
};
iterator &begin();
iterator &end();
};
template<class T>
struct mytype {
T a;
T b;
};
template<class T>
class Foo {
int abc() {
myvector<mytype<T foo;
// DOES work:
//for (myvector<mytype<int::iterator iter = foo.begin();
iter != foo.end(); ++iter) {
// DOES NOT work:
for (myvector<mytype<T::iterator iter = foo.begin(); iter !
= foo.end(); ++iter) {
// ...
}
return 0;
}
};
---------------------------------------------------------------------
In method Foo::abc if I specify int as template argument for mytype,
everything works, but if I want to use T (the template argument of Foo
class), the compiler reports this error:
foo.cpp: In member function 'int Foo<T>::abc()':
foo.cpp:28: error: expected `;' before 'iter'
foo.cpp:28: error: 'iter' was not declared in this scope
I haven't personally testes, but apparently the Microsoft compiler
(Visual Studio 2005) accept that code.
Any help is appreciated!
Thanks,
Fabrizio

Whew! Now this will work. The main problem was the typename
keyword was required. To get the program to be errorless was
a another story :)

HTH

-----------------------------------------------------------
template<class T>
class myvector
{ // Just a simplification of std::vector
public:
class iterator
{
public:
T a;
bool operator!=( const iterator &b ) { return true; }
iterator & operator++() { return *this; }
iterator operator++( int ) { return *this; }
};

iterator begin() { return iterator(); }
iterator end() { return iterator(); }

};

template<class T>
struct mytype {
T a;
T b;

};

template<class T>
class Foo
{
public:
void abc()
{
myvector<mytype<T foo;
// DOES work:
//for (myvector<mytype<int::iterator iter = foo.begin();
//iter != foo.end(); ++iter) {

// DOES NOT work:
for (typename myvector<mytype<T::iterator iter = foo.begin();
iter != foo.end(); ++iter)
{

}

}

};

int
main()
{
Foo<inttest;
test.abc();
return 0;

}

Mar 2 '07 #5
for( typename myvector<mytype<T::iterator...

This occurs so frequently that I propose the 80/20 rule:
80% of all template problems posted to comp.lang.c++ are due to a
missing 'typename' keyword. 20% are other problems.

Michael

Mar 2 '07 #6
Michael wrote:
>for( typename myvector<mytype<T::iterator...

This occurs so frequently that I propose the 80/20 rule:
80% of all template problems posted to comp.lang.c++ are due to a
missing 'typename' keyword. 20% are other problems.

Michael
Somewhat surprisingly, we don't have anything about this in the FAQ
(that I can find). 35.18 sort of covers it, but not in a way that
someone looking for the answer to this variation of the problem would be
likely to understand.
Mar 2 '07 #7
Alan Johnson wrote:
Michael wrote:
>>for( typename myvector<mytype<T::iterator...

This occurs so frequently that I propose the 80/20 rule:
80% of all template problems posted to comp.lang.c++ are due to a
missing 'typename' keyword. 20% are other problems.

Michael

Somewhat surprisingly, we don't have anything about this in the FAQ
(that I can find). 35.18 sort of covers it, but not in a way that
someone looking for the answer to this variation of the problem would be
likely to understand.
Indeed, when I wrote my reply, I began, "This is an FAQ," and went off
to search for the relevant link item. Then I discovered the same thing
you did. (Now I didn't try very hard with the search feature so maybe
it's categorized under one of the miscellaneous chapters.)
Mar 2 '07 #8
Mark P <us****@fall2005remove.fastmailcaps.fmwrote:
Alan Johnson wrote:
>Michael wrote:
>>This occurs so frequently that I propose the 80/20 rule:
80% of all template problems posted to comp.lang.c++ are due to a
missing 'typename' keyword. 20% are other problems.

Somewhat surprisingly, we don't have anything about this in the FAQ
(that I can find). 35.18 sort of covers it, but not in a way that
someone looking for the answer to this variation of the problem would be
likely to understand.

Indeed, when I wrote my reply, I began, "This is an FAQ," and went off
to search for the relevant link item. Then I discovered the same thing
you did. (Now I didn't try very hard with the search feature so maybe
it's categorized under one of the miscellaneous chapters.)
I agree. I just sent an email to Marshall Cline suggesting that it be
added, and pointed him to this thread.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 2 '07 #9

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

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
1
by: matz2k | last post by:
I've got a big problem with the CSS layout which I've produced with Photoshop/Dreamweaver especially for my ebay auctions. This is what it looks like...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.