Reputation: 15718
I'm sorry but there can't be an example so we'll have to take a look into the real code here.
What happens is, I have class CItemElem
(note this is an old source, please disconsider hungarian notations, class names etc..).
As in the example below I'm trying to do the same with CItemElem
class A
{
public:
int value;
};
int _tmain( int argc, _TCHAR* argv[] )
{
std::vector<A> hold;
A a;
hold.push_back(a);
}
Althought with class CItemElem
the compiler gives me C2679 when trying to
vector<CItemElem>hold;
CItemElem item;
hold.push_back(item);
Error C2679: '=' binary :no operator found which takes a right-hand operand of type 'const CItemElem' (or there is no acceptable conversion)
by clicking at the error it leads me to this line *_First = _Val;
at this function on xutility
template<class _FwdIt,
class _Ty> inline
void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
{ // copy _Val through [_First, _Last)
for (; _First != _Last; ++_First)
*_First = _Val;
}
The CItemElem class is too long and derived so I decided to upload it to pastebin, instead of pasting the huge code in here. Pastebin: Class CItemElem Header (item.h)
Note that CItemElem is derived from CItemBase and has = operator overloaded, which also passes throught CItemBase = operator. This is from item.cpp
:
CItemElem& CItemElem::operator =( CItemElem & ie )
{
CItemBase::operator =( ie );
Upvotes: 0
Views: 223
Reputation: 99094
It looks as if there is no defined assignment operator (=
) for CItemElem
. The STL containers (e.g.vector
) expect the things they contain to have certain properties, including an assignment operator. If you can modify the class, you can add one:
class CItemElem
{
...
public:
CItemElem & operator=(const CItemElem &other)
{
// copy whatever must be copied
x = other.x;
return *this;
}
}
EDIT:
I see now that the header includes a declaration of an assignment operator:
virtual CItemElem& operator = ( CItemElem & ie );
but the signature is wrong-- it's missing const
. If you can change this (in the declaration and the definition) it should work.
EDIT:
If you can't edit the base class, you have a couple of options. Probably the safest is to copy the code from CItemBase::operator=
into CItemElem::operator=
. It's not pretty, but that's the original author's fault; the argument should have been const &
all along.
Upvotes: 3
Reputation: 146910
CItemElem has operator=(CItemElem&)
. This function cannot accept a const
argument (as the compiler accurately told you) and does not meet the requirements of std::vector
as an element type. It is impossible to use this class in a std::vector
.
Upvotes: 2