g++ template parameter error(g++模板参数错误)
问题描述
我有如下 GetContainer() 函数.
I have GetContainer() function as follows.
template<typename I,typename T,typename Container>
Container& ObjCollection<I,T,Container>::GetContainer()
{
return mContainer;
}
当我如下使用这个方法时
When I use this method as follows
template<typename I,typename T>
T& DynamicObjCollection<I,T>::Insert(T& t)
{
GetContainer().insert(&t);
return t;
}
我遇到了错误.
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)
它在 MSVC 上运行良好,但 g++ 不是那么宽松.代码有什么问题?
It works fine with MSVC, but g++ is not so permissive. What's wrong with the code?
推荐答案
我注意到GetContainer
函数是ObjCollection
的一个方法,而Insert
是 DynamicObjectCollection
的成员.由此,我将假设 DynamicObjectCollection
继承自 ObjectCollection
.
I noticed that the GetContainer
function is a method of ObjCollection
, while Insert
is a member of DynamicObjectCollection
. From this, I'm going to assume that DynamicObjectCollection
inherits from ObjectCollection
.
如果确实如此,那么问题是当你编写一个从模板基类继承的模板类时,名称查找的工作方式与普通类中的名称查找略有不同.特别是,您不能只使用名称来引用基类成员;您需要向编译器指明在哪里查找名称.这在 Visual Studio 中有效的原因是 Microsoft C++ 编译器实际上错误地处理了这种行为,并允许在技术上非法的代码编译得很好.
If this is indeed the case, the problem is that when you write a template class that inherits from a template base class, the way that name lookup works is slightly different from name lookup in normal classes. In particular, you cannot just reference base class members using their names; you need to indicate to the compiler where to look for the name. The reason this works in Visual Studio is that the Microsoft C++ compiler actually gets this behavior wrong and allows code that is technically illegal to compile just fine.
如果要调用基类的GetContainer
函数,有两种选择.首先,您可以明确指出该调用是对成员函数的:
If you want to invoke the GetContainer
function of the base class, you have two options. First, you can explicitly indicate that the call is to a member function:
this->GetContainer().insert(&t);
现在编译器知道 GetContainer
是 DynamicObjectCollection
的成员,它知道它可能需要在基中查找 GetContainer
类,因此它将推迟名称查找,直到模板被实例化.
Now that the compiler knows that GetContainer
is a member of DynamicObjectCollection
, it knows that it might need to look up GetContainer
in the base class, and so it will defer name lookup until the template is instantiated.
另一个可用的选项是在类主体中添加 using
声明:
The other option available would be to add a using
declaration into the class body:
template <typename I, typename T>
class DynamicObjectCollection: public ObjectCollection<I, T, /* ? */> {
public:
using ObjectCollection<I, T, /* ? */>::GetContainer;
/* ... */
};
这也明确地向编译器表明 GetContainer
可以在基类中定义,因此它将查找推迟到模板实例化.
This also indicates unambiguously to the compiler that GetContainer
may be defined in the base class, and so it defers lookup until template instantiation.
如果这不适用于您的情况,请告诉我,我可以删除此帖子.
If this isn't applicable to your situation, let me know and I can delete this post.
希望这会有所帮助!
这篇关于g++模板参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:g++模板参数错误
基础教程推荐
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01