本文介绍了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
{
}

推荐答案



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 ++模板类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 10:48