C ++模板类构造函数 [英] C++ template class constructor

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

问题描述

你好,

我使用仅布尔值的简单构造函数创建了以下模板类.

当我要实例化该类的派生类时,无法传递布尔值.
任何想法我在做什么错:

我有以下错误:

错误C2664:"CPointerList< T> :: CPointerList(const CPointerList< T&)'':无法将参数1从"bool"转换为"const CPointerList< T>". &''

[
T = CP7FAirHinge *
]
原因:无法从布尔"转换为"const CPointerList< T>"

[
T = CP7FAirHinge *
]
没有构造函数可以采用源类型,或者构造函数重载解析度不明确


Hello,

I created following template class with easy constructor with only boolean.

When I want to instantiate an derived class of this one, I cannot pass the boolean.
Any ideas what I''m doing wrong:

I have following error:

error C2664: ''CPointerList<T>::CPointerList(const CPointerList<T> &)'' : cannot convert parameter 1 from ''bool'' to ''const CPointerList<T> &''
with
[
T=CP7FAirHinge *
]
Reason: cannot convert from ''bool'' to ''const CPointerList<T>''
with
[
T=CP7FAirHinge *
]
No constructor could take the source type, or constructor overload resolution was ambiguous


template <class t>
class CPointerList : public std::list<t>
{
private:
	bool mbvIsOwner;
public:
	CPointerList(bool IsOwner)  { mbvIsOwner = IsOwner; }
	virtual ~CPointerList(){ ClearWithContents(); }

	void ClearWithContents()
	{
		if(!mbvIsOwner) return;	//only clear with content if you are owner

		iterator lovIt = begin();
		iterator lovIt_End = end();

		for ( ; lovIt!=lovIt_End; ++lovIt)
		{
			delete *lovIt;
		}
		clear();
	}
}

//some class who derives from the template

class P7F_GDATALIB_API CP7FAirHingeRefList : public CPointerList<cp7fairhinge*>
{	
public:
	CP7FAirHingeRefList();
}

//implementation of constructor of derived class in cpp
CP7FAirHingeRefList::CP7FAirHingeRefList()
: CPointerList<cp7fairhinge*>(false)   //this line gives compilation error
{
}

推荐答案

我有另一个主意:

由于错误消息显然试图找到复制构造函数,也许您已在代码中的某个位置尝试从函数返回CP7FAirHingeRefList的实例或将其按值传递给函数?如果是这样,那将需要一个副本构造函数的存在,并且由于您没有提供一个副本构造函数,因此编译器将采用它自动创建的副本构造函数,这将依次尝试调用基类CPointerList的副本构造函数. br/>
建议的解决方案:为CP7FAirHingeRefList提供一个副本构造函数.
I have another idea:

Since the error message apparently tries to find a copy constructor, maybe somewhere in your code you''ve tried to return an instance of CP7FAirHingeRefList from a function or pass it to a function by value? If so, that would require the existance of a copy constructor, and since you didn''t provide one the compiler takes the one it automatically creates, which will in turn try to call the copy constructor of the base class CPointerList.

Suggested solution: Provide a copy constructor for CP7FAirHingeRefList.


对不起,我复制了错误的代码.我将问题更新为正确的内容.
因此CP7FAirHingeRefList是从CPointerList< t>
派生的
但是我无法使用boolean
Sorry, I copied wrong piece of code. I updated my question to the right piece.
So CP7FAirHingeRefList is derived from CPointerList<t>

but i cannot instantiate it with the boolean


实例化它,看不到任何问题!
Cant see any problems!
typedef void* cp7fairhinge;
#define  P7F_GDATALIB_API  

template <class t>
class CPointerList : public std::list<t>
{
private:
  bool mbvIsOwner;
public:
  CPointerList(bool IsOwner)  { mbvIsOwner = IsOwner; }
  virtual ~CPointerList(){ ClearWithContents(); }
 
  void ClearWithContents()
  {
    if(!mbvIsOwner) return;  //only clear with content if you are owner

    iterator lovIt = begin();
    iterator lovIt_End = end();
 
    for ( ; lovIt!=lovIt_End; ++lovIt)
    {
      delete *lovIt;
    }
    clear();
  }
};
 
//some class who derives from the template
class P7F_GDATALIB_API CP7FAirHingeRefList : public CPointerList<cp7fairhinge*>
{  
public:
  CP7FAirHingeRefList();
};
 
//implementation of constructor of derived class in cpp
CP7FAirHingeRefList::CP7FAirHingeRefList()
: CPointerList<cp7fairhinge*>(false)   //this line gives compilation error
{
}


int _tmain(int argc, _TCHAR* argv[])
{
  CP7FAirHingeRefList  l;
  return 0;
}


问候.


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

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