g++模板参数错误 [英] g++ template parameter error

查看:27
本文介绍了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的一个方法,而InsertDynamicObjectCollection 的成员.由此,我将假设 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);

现在编译器知道 GetContainerDynamicObjectCollection 的成员,它知道它可能需要在基中查找 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++模板参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆