|
|
代码如下[php]#include <iostream>
using namespace std;
template <class T>
class A
{
public:
A(T _x) : x(_x) {}
protected:
T x;
};
template <class T>
class B : public A<T>
{
public:
B(T _x) : A<T>(_x) {}
void Output()
{ cout<<x<<endl;} //这行出错
};
int main()
{
B<int> bb(66);
bb.Output();
return 0;
}
[/php]
编译的时候出现如下错误[php]$ g++ -o test test.cpp
test.cpp: In member function ‘void B<T>::Output()’:
test.cpp:20: 错误:‘x’ 在此作用域中尚未声明
[/php]
但是若将出错的 x 改为 this->x 则能正常编译
gcc 版本[php]$ g++ -v
使用内建 specs。
目标:i686-pc-linux-gnu
配置为:/var/tmp/portage/gcc-4.1.1-r1/work/gcc-4.1.1/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/4.1.1 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.1 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.1/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.1/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/include/g++-v4 --host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --disable-multilib --disable-libmudflap --disable-libssp --disable-libgcj --enable-languages=c,c++,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
线程模型:posix
gcc 版本 4.1.1 (Gentoo 4.1.1-r1)
[/php] |
|