s*****k 发帖数: 604 | 1 在子类 Deriv中使用父类的数据成员numArray就会出错,
提示的错误信息是numArray没有declare.
改成 Base<_Tp>::numArray 就编译通过了。
请问这是为什么?
我用的编译器是 gcc 3.4.4 (cygwin) 和 gcc 4.2.4 (ubuntu)
#include
template
class Base
{
protected:
_Tp numArray[3];
Base()
{
numArray[0] = static_cast<_Tp>(1);
numArray[1] = static_cast<_Tp>(11);
numArray[2] = static_cast<_Tp>(111);
};
};
template
class Deriv: public Base<_Tp> {
public:
Deriv() {
/* Why the followi |
S*********g 发帖数: 5298 | 2 用this->numArray
【在 s*****k 的大作中提到】 : 在子类 Deriv中使用父类的数据成员numArray就会出错, : 提示的错误信息是numArray没有declare. : 改成 Base<_Tp>::numArray 就编译通过了。 : 请问这是为什么? : 我用的编译器是 gcc 3.4.4 (cygwin) 和 gcc 4.2.4 (ubuntu) : #include : template : class Base : { : protected:
|
s*****k 发帖数: 604 | 3 有什么原因吗?
【在 S*********g 的大作中提到】 : 用this->numArray
|
s*****k 发帖数: 604 | 4 好像不是模板类的话直接用numArray就没什么问题
【在 S*********g 的大作中提到】 : 用this->numArray
|
E*****7 发帖数: 128 | 5 Both versions compile and run under Visual Studio 2005.
It would be safer to define the ctor of the deriv as:
deriv() : Base() // explicitly call the ctor of Base
{ ....} |
t****t 发帖数: 6806 | 6 因为编译器不知道到哪里去找numarray
base类是个模板, 在template parameter确定以前, 不能肯定有什么, 所以缺省是不找
base类的. 你必须显式声明numarray 是个dependent name, 才能让编译器暂时忽略这
个找不到的名字.
【在 s*****k 的大作中提到】 : 好像不是模板类的话直接用numArray就没什么问题
|
s*****k 发帖数: 604 | 7 刚用borland c++ compiler 编译就没问题。算是g++的bug吗?
【在 s*****k 的大作中提到】 : 在子类 Deriv中使用父类的数据成员numArray就会出错, : 提示的错误信息是numArray没有declare. : 改成 Base<_Tp>::numArray 就编译通过了。 : 请问这是为什么? : 我用的编译器是 gcc 3.4.4 (cygwin) 和 gcc 4.2.4 (ubuntu) : #include : template : class Base : { : protected:
|
t****t 发帖数: 6806 | 8 why it's safer?
【在 E*****7 的大作中提到】 : Both versions compile and run under Visual Studio 2005. : It would be safer to define the ctor of the deriv as: : deriv() : Base() // explicitly call the ctor of Base : { ....}
|
t****t 发帖数: 6806 | 9 恰恰相反, 只有g++在这一点上是正确的
【在 s*****k 的大作中提到】 : 刚用borland c++ compiler 编译就没问题。算是g++的bug吗?
|
E*****7 发帖数: 128 | 10 Both versions compile and run under Visual Studio 2005. |
|
|
t****t 发帖数: 6806 | 11 VC is notorious in not conforming to C++ standard. g++ is much closer.
【在 E*****7 的大作中提到】 : Both versions compile and run under Visual Studio 2005.
|
n****y 发帖数: 106 | 12 这个是effective c++ 的item 43
但是在编译的时候,不是已经知道template parameter了吗?template不是only
compile when needed吗?
给详细讲讲?
【在 t****t 的大作中提到】 : 因为编译器不知道到哪里去找numarray : base类是个模板, 在template parameter确定以前, 不能肯定有什么, 所以缺省是不找 : base类的. 你必须显式声明numarray 是个dependent name, 才能让编译器暂时忽略这 : 个找不到的名字.
|
t****t 发帖数: 6806 | 13 compile有很多步骤的
新一点的的编译器会先检查语法, 做name lookup, dependent name会在name lookup时
忽略
【在 n****y 的大作中提到】 : 这个是effective c++ 的item 43 : 但是在编译的时候,不是已经知道template parameter了吗?template不是only : compile when needed吗? : 给详细讲讲?
|
n****y 发帖数: 106 | |
l**b 发帖数: 1 | 15 When compiler encounters a template, it does name look up in two steps. For
all types that are non-template dependent, it checks at the time the
template is defined. For all template dependent types, it has to wait until
the template is instantiated to do name loop up. Since your array does not
have any qualifier to tell compiler that it is template type dependent,
compiler assumes it is not and tried to resolve the name look up and
declared error.
That is also why if you add this or Base<_tp>::
【在 n****y 的大作中提到】 : 还是不太明白
|