C++ 对依赖基类成员的非限定访问将导致;必须提供[x]的声明;

C++ 对依赖基类成员的非限定访问将导致;必须提供[x]的声明;,c++,templates,stack,C++,Templates,Stack,代码: //test3.cpp #包括 使用名称空间std; 模板 结构ptr_stack_tp; 模板 struct ptr_stack_tp:公共堆栈 { ~ptr_stack_tp() { 而(!empty()){ 运算符删除(top()); pop(); } } }; int main() {} 错误消息(gcc 4.7.2): test3.cpp:在析构函数“ptr_stack_tp::~ptr_stack_tp()”中: test3.cpp:15:23:错误:没有依赖于模板参数的'

代码:

//test3.cpp
#包括
使用名称空间std;
模板
结构ptr_stack_tp;
模板
struct ptr_stack_tp:公共堆栈
{
~ptr_stack_tp()
{
而(!empty()){
运算符删除(top());
pop();
}
}
};
int main()
{}
错误消息(gcc 4.7.2):

test3.cpp:在析构函数“ptr_stack_tp::~ptr_stack_tp()”中:
test3.cpp:15:23:错误:没有依赖于模板参数的'empty'参数,因此'empty'声明必须可用[-fppermissive]
test3.cpp:15:23:注意:(如果您使用'-fppermissive',G++将接受您的代码,但不赞成使用未声明的名称)
test3.cpp:16:33:错误:没有依赖于模板参数的'top'参数,因此'top'的声明必须可用[-fppermissive]
test3.cpp:17:17:错误:“pop”没有依赖于模板参数的参数,因此“pop”的声明必须可用[-fppermissive]

函数
empty()
top()
pop()
都是
std::stack
的函数,那么,为什么gcc找不到它们呢?

您应该通过
this
指针显式调用类模板中的基类成员函数

test3.cpp: In destructor 'ptr_stack_tp<T*>::~ptr_stack_tp()':
test3.cpp:15:23: error: there are no arguments to 'empty' that depend on a template parameter, so a declaration of 'empty' must be available [-fpermissive]
test3.cpp:15:23: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
test3.cpp:16:33: error: there are no arguments to 'top' that depend on a template parameter, so a declaration of 'top' must be available [-fpermissive]
test3.cpp:17:17: error: there are no arguments to 'pop' that depend on a template parameter, so a declaration of 'pop' must be available [-fpermissive]
/。。。
模板
struct ptr_stack_tp:公共堆栈
{
~ptr_stack_tp()
{
而(!this->empty()){
//      ^^^^^^
运算符删除(此->顶部());
//              ^^^^^^
这个->弹出();
//  ^^^^^^
}
}
};
// ...
这是由于模板的两阶段名称查找工作方式造成的。如果没有
this->
间接寻址,编译器将尝试将非限定名解析为全局函数名。由于不存在名为
empty()
top()
pop()
的全局函数,编译器将发出错误


当您使用
this->
时,编译器会将名称查找延迟到模板实际实例化时:此时,对基类成员的函数调用将被正确解析。

这是一个依赖名称,
this->empty()
等。此代码有风险。如果将
ptr\u stack\u tp
作为指向
stack
的指针传递,并调用
delete
,您将得到(可能)难以发现的巨大内存泄漏。不要从标准库类派生,而是通过组合实现它们的行为。确实很简单。谢谢。有关更多信息,请看一看(你的问题是重复的)。@Peregring lk:不客气。这个答案能解决你的问题吗?
test3.cpp: In destructor 'ptr_stack_tp<T*>::~ptr_stack_tp()':
test3.cpp:15:23: error: there are no arguments to 'empty' that depend on a template parameter, so a declaration of 'empty' must be available [-fpermissive]
test3.cpp:15:23: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
test3.cpp:16:33: error: there are no arguments to 'top' that depend on a template parameter, so a declaration of 'top' must be available [-fpermissive]
test3.cpp:17:17: error: there are no arguments to 'pop' that depend on a template parameter, so a declaration of 'pop' must be available [-fpermissive]
// ...

template<typename T>
struct ptr_stack_tp<T*> : public stack<T*>
{
    ~ptr_stack_tp()
    {
        while (!this->empty()) {
        //      ^^^^^^
            operator delete(this->top());
            //              ^^^^^^
            this->pop();
        //  ^^^^^^
        }
    }
};

// ...