本文介绍了STL矢量类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否可以插入一个类< vec>它有一个向量< double>

成员,进入向量< vec> veclist for eg?


我尝试使用

向量方法插入时遇到编译错误push_back() - >


c:\ program program \ microsoft visual studio\vc98 \include \xutility(39):

error C2679:binary''='':no运算符定义哪个是右手

类型''const class vec''的操作数(或者没有可接受的

转换)


c:\ program Files \ microsoft visual studio\vc98\include\vector(170):参见

对函数模板实例化的引用''void __cdecl

std :: fill(类vec *,类vec *,const类vec&)''正在编译


c:\program files\microsoft visual studio \ vc98 \include \ xmemory(34):

错误C2558:类''vec'':没有复制构造函数


c:\program files \微软视觉工作室\\ vc98 \include \ xmemory(66):参见

对函数模板实例化的引用''void __cdecl

std :: _ Construct(类vec *,const类vec &)''正在编译

执行cl.exe时出错。

我已经有一个重载的运算符=我的vec类而且我没有

真正了解错误的位置。


这是我的源代码:


class< vec>头文件


public:


vec();

vec(vec&);

vec(int,int); // dimension,vecid

vec(int,int,vector< double>); //维度,vecid,coords

vec(int,int,vector< double>,vector< int>); //维度,vecid,

coords,连接vecs列表

~vec();


void setvecid(int) ;

void setveccoord(vector< double>);

void setvecconnect(vector< int>);

void setvec(int,int, vector< double>,vector< int>);

void setvec(vec&);

int getdim();

int getvecid( );

vector< double> getcoord(); //得到点坐标

vector< int> getvecconnect(); //获取连接的vecs列表

void addconnectvec(int); //将连接的vec添加到vecconnect列表

void delconnectvec(int); //从列表中删除已连接的vec

bool searchvec(int); //在vecconnect列表中搜索vec

void sortvecconnect(); //对

vec运算符中的列表进行排序=(vec&);

在我的main()中,我声明一个全局变量:


vector< vec> globalvecs;


当我使用语句时出现编译错误:


globalvecs.push_back(tmpvec);


在以下代码块中:


for(i = 0; i< numVec; i ++)

{

vec tmpvec(dimension,i); // temp vec用于添加到全局veclist

vector< double> tmpcoord; //矢量来保存临界坐标b4设置


// tmpvec'的坐标


for(j = 0; j< dimension ; j ++)

{

read>> X; //其中x是double类型

tmpcoord.push_back(x);

}

tmpvec.setveccoord(tmpcoord);


globalvecs.push_back(tmpvec);


}


预先感谢您的回复!

问候,


sw

Hi,

Is it possible to insert a class <vec> which has a vector<double>
member, into the vector<vec> veclist for e.g?

I''ve been getting compilation errors when trying to insert using the
vector method push_back() ->

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 vec'' (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 vec *,class vec *,const class vec &)'' being compiled

c:\program files\microsoft visual studio\vc98\include\xmemory(34) :
error C2558: class ''vec'' : no copy constructor available

c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see
reference to function template instantiation ''void __cdecl
std::_Construct(class vec *,const class vec &)'' being compiled
Error executing cl.exe.
I''ve already had an overloaded operator= for my vec class and i dont
really understand where the error lies in.

Here is my source code:

class <vec> header file

public:

vec();
vec(vec&);
vec(int, int); //dimension, vecid
vec(int, int, vector<double>); //dimension, vecid, coords
vec(int, int, vector<double>, vector<int>); //dimension, vecid,
coords, list of conected vecs
~vec();

void setvecid(int);
void setveccoord(vector<double>);
void setvecconnect(vector<int>);
void setvec(int, int, vector<double>, vector<int>);
void setvec(vec&);
int getdim();
int getvecid();
vector<double> getcoord(); //get the point coordinates
vector<int> getvecconnect(); //get the list of connected vecs
void addconnectvec(int); //add connected vec to the vecconnect list
void delconnectvec(int); //remove connected vec from list
bool searchvec(int); //search for vec in the vecconnect list
void sortvecconnect(); //sort the list in the

vec operator=(vec&);
In my main(), i declare a global variable:

vector<vec> globalvecs;

I get the compile error when i use the statement:

globalvecs.push_back(tmpvec);

inside the following block of code:

for(i = 0; i < numVec; i++)
{
vec tmpvec(dimension, i); //temp vec used to add to the global veclist
vector<double> tmpcoord; //vector to hold temp coordinates b4 setting

//tmpvec''s coord

for(j = 0; j < dimension; j++)
{
read >> x; //where x is of type double
tmpcoord.push_back(x);
}
tmpvec.setveccoord(tmpcoord);

globalvecs.push_back(tmpvec);

}

Advance thanks for your replies!
Regards,

sw

推荐答案





vec& operator =(const vec&);


:: A ::



vec& operator=(const vec&);

::A::





与上面相同。编译器非常清楚地说明了这一点:

没有运算符定义,它采用类型为'

类vec''的右手操作数"


你的operator = signature表明它将修改赋值的右侧

一侧(即它不是const),并且向量想要

一个不这样做。


另一件事:不要让operator =按值返回。这意味着它将创建整个vec的另一个副本(使用复制构造函数)

来生成返回值。成功:


vec& operator =(const vec&);





const vec& operator =(const vec&);



Same as above. The compiler says it quite explicitly:
"no operator defined which takes a right-hand operand of type ''const
class vec''"

Your operator= signature suggests that it will modify the right-hand
side of the assignment (i.e. it is not const), and the vector wants
one that doesn''t do that.

Another thing: Don''t make operator= return by value. This means that it
will create another copy of the whole vec (using the copy constructor)
for generating the return value. Make it:

vec& operator=(const vec&);

or

const vec& operator=(const vec&);


这篇关于STL矢量类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:53