PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > gcc 4 can not resolve member var derived from base class in atemplate class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
gcc 4 can not resolve member var derived from base class in atemplate class

Réponse
 
LinkBack Outils de la discussion
Vieux 01/07/2008, 17h05   #1
steve yee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut gcc 4 can not resolve member var derived from base class in atemplate class

#include <iostream>
#include <list>

template<class mt_policy>
class Base
{
public:
int a;
};

template<class mt_policy = int>
class signal0 : public Base<mt_policy>
{
public:
void aaa()
{
std::cout<<"a = "<<a<<std::endl;
}
};

int main()
{
signal0<int> p;
p.aaa();
}
~

test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
test.cpp:17: error: ‘a’ was not declared in this scope
  Réponse avec citation
Vieux 01/07/2008, 18h10   #2
acehreli@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gcc 4 can not resolve member var derived from base class in atemplate class

On Jul 1, 9:05 am, steve yee <yiton...@gmail.com> wrote:
> #include <iostream>
> #include <list>
>
> template<class mt_policy>
> class Base
> {
> public:
> int a;
>
> };
>
> template<class mt_policy = int>
> class signal0 : public Base<mt_policy>
> {
> public:
> void aaa()
> {
> std::cout<<"a = "<<a<<std::endl;


This is the definition of class template signal0, not an instantiation
of it. So the compiler cannot know whether any Base<mt_policy> will
provide an 'a'. For example, some specialization may not have an a.

The compiler cannot assume that a missing name will be available
through a templated base. What if there were two bases?

The solution is to fully qualify 'a': Base<mt_policy>::a

> }
>
> };
>
> int main()
> {
> signal0<int> p;
> p.aaa();}
>
> ~
>
> test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
> test.cpp:17: error: ‘a’ was not declared in this scope


Ali
  Réponse avec citation
Vieux 01/07/2008, 19h00   #3
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gcc 4 can not resolve member var derived from base class in atemplate class

steve yee wrote:
> test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
> test.cpp:17: error: ‘a’ was not declared in this scope


You have to add this to your signal0 class:

using Base<mt_policy>::a;

I don't remember now why this was standardized like that.
  Réponse avec citation
Vieux 01/07/2008, 19h17   #4
steve yee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gcc 4 can not resolve member var derived from base class in atemplate class


so this is a standard, not a bug of gcc? but vc9 can compile it.


On Jul 2, 2:00am, Juha Nieminen <nos...@thanks.invalid> wrote:
> steve yee wrote:
> > test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
> > test.cpp:17: error: ‘a’ was not declared in this scope

>
> You have to add this to your signal0 class:
>
> using Base<mt_policy>::a;
>
> I don't remember now why this was standardized like that.


  Réponse avec citation
Vieux 01/07/2008, 19h37   #5
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gcc 4 can not resolve member var derived from base class in atemplate class

steve yee wrote:
> so this is a standard, not a bug of gcc? but vc9 can compile it.


VC9 doesn't obey the standard in all respects.
  Réponse avec citation
Vieux 01/07/2008, 19h47   #6
Kyle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gcc 4 can not resolve member var derived from base class in atemplate class

steve yee wrote:
> #include<iostream>
> #include<list>
>
> template<class mt_policy>
> class Base
> {
> public:
> int a;
> };
>
> template<class mt_policy = int>
> class signal0 : public Base<mt_policy>
> {
> public:
> void aaa()
> {
> std::cout<<"a ="<<a<<std::endl;


here you need to make name 'a' dependent on template parameter
one of ways to do this, is to access 'a' through this pointer:

std::cout<<"a ="<<this->a<<std::endl;

> }
> };
>
> int main()
> {
> signal0<int> p;
> p.aaa();
> }
> ~
>
> test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
> test.cpp:17: error: ‘a’ was not declared in this scope


  Réponse avec citation
Vieux 01/07/2008, 22h31   #7
Bo Persson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: gcc 4 can not resolve member var derived from base class in a template class

Juha Nieminen wrote:
> steve yee wrote:
>> so this is a standard, not a bug of gcc? but vc9 can compile it.

>
> VC9 doesn't obey the standard in all respects.


It does respect this, if you ask it too (option /Za). Default is to
compile old, pre-standard, Windows code.


Bo Persson


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12631 seconds with 15 queries