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

查看:110
本文介绍了g ++模板参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有GetContainer()函数如下。

  template< typename I,typename T,typename Container> 
容器& ObjCollection< I,T,Container> :: GetContainer()
{
return mContainer;
}

当我使用此方法如下

  template< typename I,typename T> 
T& DynamicObjCollection< I,T> :: Insert(T& t)
{
GetContainer()。insert(& t);
return t;
}

我遇到错误。

 错误:没有依赖于模板参数的GetContainer参数
因此必须提供GetContainer的声明

错误:(如果使用'-fpermissive',G ++将接受你的代码,但允许使用
一个未声明的名称已被弃用)

它适用于MSVC,但g ++不是那么宽容。

我注意到, GetContainer 函数是什么错误的?

解决方案是 ObjCollection 的一个方法,而插入 DynamicObjectCollection 。从这里,我将假设 DynamicObjectCollection 继承自 ObjectCollection



如果情况确实如此,问题是当你编写一个从模板基类继承的模板类时,名称查找的工作方式与普通类中的名称查找略有不同。特别是,你不能仅仅使用它们的名称来引用基类成员;您需要向编译器指示在哪里查找名称。在Visual Studio中工作的原因是,Microsoft C ++编译器实际上得到这种行为错误,并允许代码在技术上是非法的编译只是罚款。



如果你想调用基础类的 GetContainer 函数,你有两个选项。首先,你可以明确地指出调用是一个成员函数:

  this-> GetContainer ; t); 

现在编译器知道 GetContainer DynamicObjectCollection 的成员,它知道它可能需要在基类中查找 GetContainer ,因此它将



另一个选项是使用声明将添加到类主体:

  template< typename I,typename T& 
class DynamicObjectCollection:public ObjectCollection< I,T,/ *? * /> {
public:
使用ObjectCollection< I,T,/ *? * /> :: GetContainer;

/ * ... * /
};

这也明确告诉编译器 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;
}

I got errors.

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)

It works fine with MSVC, but g++ is not so permissive. What's wrong with the code?

解决方案

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.

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.

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);

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.

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;

    /* ... */
};

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.

Hope this helps!

这篇关于g ++模板参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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