|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
#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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|