public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: STL iterator problem
@ 2004-11-24 15:02 Martin York
  0 siblings, 0 replies; 5+ messages in thread
From: Martin York @ 2004-11-24 15:02 UTC (permalink / raw)
  To: kini1982, gcc-help

Here is what I have found.
Hope this helps.

> for(B::iterator i = the_Container->Begin(); i != the_Container->end();
++i  )

The problem is cause because B is a template class and g++ can not
guarantee that B::iterator is a type (could be a static member variable)
when it fist parses the function. g++ adheres strictly to the standard
which requires that in these situations you use the typename keyword to
disambigiufy the situation. Sorry can not quote chapter and verse on
what part of the standard.

PS. Also note that the method Begin() should probably be begin().

> for(typename B::iterator i = the_Container->begin(); i !=
the_Container->end(); ++i  )


Martin


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Yogesh Kini
Sent: 24 November 2004 03:21
To: gcc-help@gcc.gnu.org
Subject: STL iterator problem

Hi,
Please take a look at the code snippet below.
The code compiles fine with Microsoft VC++ but gives a error with gcc(as
indicated below) I am trying to port the file from msvc to gcc. Can
anyone suggest me a work around. 

Thanks
Yogesh


#include <stdio.h>
#include <list>
 
template <class B>
class parent
{
public:
	B * m_Container;			
	void funone();
	void CreateList()
	{
		m_Container = new B();  
		m_Container->push_front(1);		
		m_Container->push_front(2);		
		m_Container->push_front(3);		
		m_Container->push_front(4);		
	}
	B * GetList()
	{
		return (m_Container);
	}
};
 

template<class B>
void parent<B>::funone()
{
	B * the_Container = GetList();
	for(B::iterator i = the_Container->Begin(); i !=
the_Container->end(); ++i  )  -> Error Here: Parse error before "="
token
	{
		printf("%d ",*i);	
	}	
}
 
int main()
{ 
	parent<std::list<int>> the_instance;
	the_instance.CreateList();
	the_instance.funone();
}

Error: parse error before "=" token



___________________________________________________
Have your own email and web address for life.

http://www.homemaster.net - Homemaster. Come Together. Online.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: STL iterator problem
  2004-11-24 11:55   ` jlh
@ 2004-11-24 14:50     ` Eljay Love-Jensen
  0 siblings, 0 replies; 5+ messages in thread
From: Eljay Love-Jensen @ 2004-11-24 14:50 UTC (permalink / raw)
  To: jlh, gcc-help

Hi jlh,

 >But still I don't get why this is wrong.  Is only because the compiler 
can't tell whether it's a typename or something else at parse time?

Correct, compilers are not allowed to ASSUME (as per ISO 14882) that it is 
a typename.

Compilers that do make that ASSUMPTION are in violation of ISO 
14882.  (Which is why it is wrong.)

GCC works hard to be compliant.  EDG is often lauded as a very, very 
compliant compiler (even implementing the dusty-hard-to-reach ISO 14882 
items).  MSVC comes up short in this regard.  I'm out of experience with 
Metrowerks or Borland these days, but I have fond memories of them.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: STL iterator problem
  2004-11-24  8:29 ` Philippe Haution
@ 2004-11-24 11:55   ` jlh
  2004-11-24 14:50     ` Eljay Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: jlh @ 2004-11-24 11:55 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 690 bytes --]


> gcc is right to reject this code, something more
> standard compliant should pass :
> 
> for(typename B::iterator i; ...)

Yep, conveniently, gcc-3.4.3 even suggests it:

a.cpp:29: error: dependent-name ` B::iterator' is parsed as a non-type,
           but instantiation yields a type
a.cpp:29: note: say `typename  B::iterator' if a type is meant

But still I don't get why this is wrong.  Is only because the compiler
can't tell whether it's a typename or something else at parse time?

btw, take care with "parent<std::list<int>>".  Apparently GCC now
gets it right, but to be portable, better write "parent<std::list<int> >",
to avoid parsing a shift operator instead.

Cheers,
jlh

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: STL iterator problem
  2004-11-24  8:21 Yogesh Kini
@ 2004-11-24  8:29 ` Philippe Haution
  2004-11-24 11:55   ` jlh
  0 siblings, 1 reply; 5+ messages in thread
From: Philippe Haution @ 2004-11-24  8:29 UTC (permalink / raw)
  To: kini1982; +Cc: gcc-help

Hello,

gcc is right to reject this code, something more
standard compliant should pass :

for(typename B::iterator i; ...)

Regards,

PH
 --- Yogesh Kini <kini1982@postmaster.co.uk> a écrit :

> Hi,
> Please take a look at the code snippet below.
> The code compiles fine with Microsoft VC++ but gives
> a error with gcc(as indicated below)
> I am trying to port the file from msvc to gcc. Can
> anyone suggest me a work around. 
> 
> Thanks
> Yogesh
> 
> 
> #include <stdio.h>
> #include <list>
>  
> template <class B>
> class parent
> {
> public:
> 	B * m_Container;			
> 	void funone();
> 	void CreateList()
> 	{
> 		m_Container = new B();  
> 		m_Container->push_front(1);		
> 		m_Container->push_front(2);		
> 		m_Container->push_front(3);		
> 		m_Container->push_front(4);		
> 	}
> 	B * GetList()
> 	{
> 		return (m_Container);
> 	}
> };
>  
> 
> template<class B>
> void parent<B>::funone()
> {
> 	B * the_Container = GetList();
> 	for(B::iterator i = the_Container->Begin(); i !=
> the_Container->end(); ++i  )  -> Error Here: Parse
> error before "=" token
> 	{
> 		printf("%d ",*i);	
> 	}	
> }
>  
> int main()
> { 
> 	parent<std::list<int>> the_instance;
> 	the_instance.CreateList();
> 	the_instance.funone();
> }
> 
> Error: parse error before "=" token
> 
> 
> 
> ___________________________________________________
> Have your own email and web address for life.
> 
> http://www.homemaster.net - Homemaster. Come
> Together. Online.
> 
>  


	

	
		
Vous manquez dÂ’espace pour stocker vos mails ? 
Yahoo! Mail vous offre GRATUITEMENT 100 Mo !
Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/

Le nouveau Yahoo! Messenger est arrivé ! Découvrez toutes les nouveautés pour dialoguer instantanément avec vos amis. A télécharger gratuitement sur http://fr.messenger.yahoo.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* STL iterator problem
@ 2004-11-24  8:21 Yogesh Kini
  2004-11-24  8:29 ` Philippe Haution
  0 siblings, 1 reply; 5+ messages in thread
From: Yogesh Kini @ 2004-11-24  8:21 UTC (permalink / raw)
  To: gcc-help

Hi,
Please take a look at the code snippet below.
The code compiles fine with Microsoft VC++ but gives a error with gcc(as indicated below)
I am trying to port the file from msvc to gcc. Can anyone suggest me a work around. 

Thanks
Yogesh


#include <stdio.h>
#include <list>
 
template <class B>
class parent
{
public:
	B * m_Container;			
	void funone();
	void CreateList()
	{
		m_Container = new B();  
		m_Container->push_front(1);		
		m_Container->push_front(2);		
		m_Container->push_front(3);		
		m_Container->push_front(4);		
	}
	B * GetList()
	{
		return (m_Container);
	}
};
 

template<class B>
void parent<B>::funone()
{
	B * the_Container = GetList();
	for(B::iterator i = the_Container->Begin(); i != the_Container->end(); ++i  )  -> Error Here: Parse error before "=" token
	{
		printf("%d ",*i);	
	}	
}
 
int main()
{ 
	parent<std::list<int>> the_instance;
	the_instance.CreateList();
	the_instance.funone();
}

Error: parse error before "=" token



___________________________________________________
Have your own email and web address for life.

http://www.homemaster.net - Homemaster. Come Together. Online.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-11-24 15:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-24 15:02 STL iterator problem Martin York
  -- strict thread matches above, loose matches on Subject: below --
2004-11-24  8:21 Yogesh Kini
2004-11-24  8:29 ` Philippe Haution
2004-11-24 11:55   ` jlh
2004-11-24 14:50     ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).