Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C++
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
{
}
Posted
Updated 23-Aug-11 13:12pm
v4
Comments
Philippe Mori 23-Aug-11 19:51pm    
What compiler are you using?

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.
 
Share this answer
 
Comments
Karel84 29-Aug-11 8:22am    
This solved my problem immedately!!!
Thx
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
 
Share this answer
 
Comments
Emilio Garavaglia 23-Aug-11 10:13am    
How doe you "instantiate it with a boolean" ??
Stefan_Lang 23-Aug-11 10:48am    
By invoking the constructor CPointerList::CPointerList(bool).
It's defined above.
Stefan_Lang 23-Aug-11 10:37am    
Hm, this code compiles ok for me. I did have to add a closing ';' at the end of each class definition though - maybe you're missing those?
Karel84 23-Aug-11 11:07am    
yeah, i have those in my solution, but did not copy the wole class and adapted it here.

I'm using VS2008, could it be compiler relative?

thx
Stefan_Lang 23-Aug-11 10:46am    
P.S.: you shouldn't post a reply to a solution (or comment) as a separate solution. Use the blue 'Have a Question or Comment?' link at the bottom right of a solution to comment on that solution, or on the black 'Reply' link at the top right of a comment (such as this one) to respond to a comment.
Cant see any problems!
C++
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;
}

Regards.
 
Share this answer
 
Comments
Philippe Mori 23-Aug-11 19:50pm    
That code does compile with Visual Studio 2010.

It is also possible to replace the line with : CPointerList(false) and it will still compile. You might try this with your compiler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900