STL problem

Started by
10 comments, last by Komillos 18 years, 10 months ago
Here's what i got ClassXYZ a; std::vector<ClassXYZ> alist; alist.push_back(a); it gives me this error : c:\program files\microsoft visual studio\vc98\include\xutility(39) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const class CShape' (or there is no acceptable conversion) c:\program files\microsoft visual studio\vc98\include\vector(170) : see reference to function template instantiation 'void __cdecl std::fill(class CShape *,class CShape *,const class CShape &)' being compiled Error executing cl.exe. Thanks in advance
Advertisement
Template error messages tend to be unhelpful.

Are you defining a custom copy constructor/assignment operator?

If so, are either of them protected or private?


If not, could you post the actual code that's causing this problem?
thanks ,

This is the definition of CShape class:
class CShape
{
public:

CShape();
~CShape();
float s_ret_w();
float s_ret_h();
float s_ret_d();

V3_s s_ret_min();
V3_s s_ret_max();

V3_s s_ret_center();

int s_ret_NOF_CP();

void s_initialize();
std::vector<V3_s> s_returnCP();

bool ed_isSelected();
void ed_MoveCenterTo(V3_s inC,int inView);

void ed_start(V3_s vStart,int inView,int inSType);
void ed_continue(V3_s vContinue,int inView);
void ed_end(V3_s vEnd,int inView);

int r_ret_NOF_V();
private:

void s_calc_h();
void s_calc_w();
void s_calc_d();
void s_calc_dim();

void s_calc_min();
void s_calc_max();
void s_calc_minmax();

void s_calc_center();

void s_initializeBOX();

int m_SType;

float m_w;
float m_h;
float m_d;

V3_s m_Vmin;
V3_s m_Vmax;

int m_NOF_CP;

V3_s m_Center;

std::vector<V3_s> m_CP;

void ed_GenerateShape(int inView);

bool m_ed_selected;

V3_s m_ed_vStart;
V3_s m_ed_vEnd;

int m_r_NOF_V;

bool m_r_renderable;
};

Constructor only calls initialization functions wich sets member variable values to default...
And this code causes the error ...

CShape a;
std::vector<CShape> al;
al.push_back(a);
it could be that you haven't defined opeartor= correctly:

class ClassXYZ {public:    ClassXYZ& operator=(const ClassXYZ& other);};


note 'other' is passed by constant reference.
i havent defined it..

in other classes this works btw
I just copy-pasted your code and it compiled...

I only added:
typedef int V3_s;
and set ctors to:
CShape() {};
~CShape() {};
since I don't know the definition of V3_s, and as you said, ctors don't do anything special..

So...?
this is definition of V3_s

struct V3_s //3D Vector
{
public:
float X,Y,Z;
void setZero() { X = Y = Z = 0.0f; };

void operator = (V3_s inVec)
{
X = inVec.X;
Y = inVec.Y;
Z = inVec.Z;
};

bool operator == (V3_s inVec)
{
if( inVec.X == X && inVec.Y == Y && inVec.Z == Z )
return true;
return false;
};
};
And everything is clear now.

void operator = (V3_s inVec){   X = inVec.X;   Y = inVec.Y;   Z = inVec.Z;};// Should be changed to:void operator = (const V3_s inVec){   X = inVec.X;   Y = inVec.Y;   Z = inVec.Z;};// Or even better and standard compliant:V3_s& operator = (const V3_s inVec){   X = inVec.X;   Y = inVec.Y;   Z = inVec.Z;   return *this;};


The reason was that you lack word const.
std::fill(class CShape *,class CShape *,const class CShape &)
needs const class CShape, to perform assignment, and it could not since one of it's members didn't have const assignment operator.

EDIT:
The order of compiling templated code is a mystery to me. Explanation:
One of the operations in operator=(CShape) is an assignment of vector<V3_s>. If the compiler tried to generate it before operator=(V3_s), it would have shout about not having operator=(const V3_s) and the bug would have been spotted instantly.
Unfortunately, it stopped earlier, realizing that there is no chance of generating operator=(const CShape). Reason given: unknown.
Quote:Original post by deffer
And everything is clear now.

*** Source Snippet Removed ***

The reason was that you lack word const.
std::fill(class CShape *,class CShape *,const class CShape &)
needs const class CShape, to perform assignment, and it could not since one of it's members didn't have const assignmet operator.

Those should be constant references.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Thanks alot , now it works , a more precise explanation of why the arguement should be declared as const?

Thanks alot , again

This topic is closed to new replies.

Advertisement