473,809 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ 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<mytyp e<int::iterato r iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytyp e<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 1886
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<mytyp e<int::iterato r iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytyp e<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<mytyp e<int::iterato r iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytyp e<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<mytyp e<int::iterato r 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...@ya hoo.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<mytyp e<int::iterato r iter = foo.begin();
iter != foo.end(); ++iter) {
// DOES NOT work:
for (myvector<mytyp e<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<mytyp e<int::iterato r 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****@fall200 5remove.fastmai lcaps.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
3347
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
31
3546
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 return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x : y;
5
2184
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 that it takes a *long* time to compile and the "long double" version instantiates so many classes that gcc runs out of memory on my 1 gig machine ! So the question is - is there a simple way to reduce the number of template instantiations from...
2
2024
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
2
4510
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
1832
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 quite answer all my questions. How would one create a 3 level TOC when each item level / node was differently named (They used Template match and for-each, but the template match worked as on a 3 level structure they usedf the same named xml...
19
7944
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 macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
3
3763
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
45
2947
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 to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
1
4470
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 http://cgi.ebay.de/ws/eBayISAPI.dll?ViewItem&item=250542673235 As you can see it can only be seen partially , can anyone help me with this? As fas as I can tell ebay's Iframe is messing up my layput, or am I wrong? Thx! here is the css/html code
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10637
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10379
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7660
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4332
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 we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.