pro*_*eek 16 c++ templates g++
我有GetContainer()函数如下.
template<typename I,typename T,typename Container>
Container& ObjCollection<I,T,Container>::GetContainer()
{
return mContainer;
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个方法如下
template<typename I,typename T>
T& DynamicObjCollection<I,T>::Insert(T& t)
{
GetContainer().insert(&t);
return t;
}
Run Code Online (Sandbox Code Playgroud)
我有错误.
error: there are no arguments to ‘GetContainer’ that depend on a template parameter,
so a declaration of ‘GetContainer’ must be available
error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of
an undeclared name is deprecated)
Run Code Online (Sandbox Code Playgroud)
它适用于MSVC,但g ++不是那么宽容.代码有什么问题?
tem*_*def 52
我注意到该GetContainer函数是一种方法ObjCollection,而它Insert是一个成员DynamicObjectCollection.由此,我将假设DynamicObjectCollection继承自ObjectCollection.
确实如此,问题在于当您编写一个继承自模板基类的模板类时,名称查找的工作方式与普通类中的名称查找略有不同.特别是,您不能仅使用其名称引用基类成员; 您需要向编译器指示在哪里查找名称.在Visual Studio中工作的原因是Microsoft C++编译器实际上会将此行为设置为错误,并允许在技术上非法的代码进行编译.
如果要调用GetContainer基类的功能,则有两个选项.首先,您可以明确指出调用是成员函数:
this->GetContainer().insert(&t);
Run Code Online (Sandbox Code Playgroud)
现在编译器知道它GetContainer是其成员DynamicObjectCollection,它知道它可能需要GetContainer在基类中查找,因此它将推迟名称查找,直到模板被实例化.
可用的另一个选项是using在类体中添加一个声明:
template <typename I, typename T>
class DynamicObjectCollection: public ObjectCollection<I, T, /* ? */> {
public:
using ObjectCollection<I, T, /* ? */>::GetContainer;
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
这也明确地指示了GetContainer可以在基类中定义的编译器,因此它将查找推迟到模板实例化.
如果这不适用于您的情况,请告诉我,我可以删除此帖子.
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
13343 次 |
| 最近记录: |