LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 951|回复: 1

关于类模板与继承的问题

[复制链接]
发表于 2006-10-18 10:47:11 | 显示全部楼层 |阅读模式
代码如下[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]
发表于 2006-10-18 17:14:22 | 显示全部楼层
看起来 g++-4.1 比 g++-3.3  更聪明。。。

模板继承来的东西不用就不编译。


我用 3.3 的编译就能通过 。。。4.1 的也不行
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表