关于复制构造函数和赋值操作符的问题

degree_37 2009-02-17 11:02:59
自己写的程序中不停的出现两个问题:
1>.\face3d.cpp(8) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const QVector3d' (or there is no acceptable conversion)
1> f:\corn\qreadobj\vector3d.h(38): could be 'QVector3d QVector3d::operator =(QVector3d &)'
1> while trying to match the argument list '(QVector3d, const QVector3d)'
1>scene3d.cpp
1>d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(481) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const QFace3d' (or there is no acceptable conversion)
1> f:\corn\qreadobj\face3d.h(21): could be 'QFace3d QFace3d::operator =(QFace3d &)'
1> while trying to match the argument list '(QFace3d, const QFace3d)'
1> d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(473) : while compiling class template member function 'void QVector<T>::append(const T &)'
1> with
1> [
1> T=QFace3d
1> ]
1> f:\corn\qreadobj\group3d.h(13) : see reference to class template instantiation 'QVector<T>' being compiled
1> with
1> [
1> T=QFace3d
1> ]
1>d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(474) : error C2558: class 'QVertex3d' : no copy constructor available or copy constructor is declared 'explicit'
1> d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(473) : while compiling class template member function 'void QVector<T>::append(const T &)'
1> with
1> [
1> T=QVertex3d
1> ]
1> f:\corn\qreadobj\scene3d.h(16) : see reference to class template instantiation 'QVector<T>' being compiled
1> with
1> [
1> T=QVertex3d
1> ]
1>d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(479) : error C2558: class 'QVertex3d' : no copy constructor available or copy constructor is declared 'explicit'
1>d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(474) : error C2558: class 'QVector3d' : no copy constructor available or copy constructor is declared 'explicit'
1> d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(473) : while compiling class template member function 'void QVector<T>::append(const T &)'
1> with
1> [
1> T=QNormal
1> ]
1> f:\corn\qreadobj\scene3d.h(17) : see reference to class template instantiation 'QVector<T>' being compiled
1> with
1> [
1> T=QNormal
1> ]
1>d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(479) : error C2558: class 'QVector3d' : no copy constructor available or copy constructor is declared 'explicit'
1>d:\qt\include\qtcore\../../src/corelib/tools/qvector.h(481) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const QNormal' (or there is no acceptable conversion)
1> f:\corn\qreadobj\vector3d.h(38): could be 'QVector3d QVector3d::operator =(QVector3d &)'
1> while trying to match the argument list '(QNormal, const QNormal)'

但是error中提示的复制构造函数和赋值操作符我明明已经在程序中定义了啊,怎么就是说没定义呢... 如下:

QVector3d QVector3d::operator =(QVector3d &vector3d)
{
m_x = vector3d.m_x;
m_y = vector3d.m_y;
m_z = vector3d.m_z;
return *this;
}
QFace3d QFace3d::operator =(QFace3d &face)
{
v0 = face.v0;
v1 = face.v1;
v2 = face.v2;
m_Normal = face.m_Normal;
}



请高手指点~
...全文
184 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dinelgua 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 degree_37 的回复:]
回1楼: 我去掉了const 还是有同样的错误 而且错误多了两个, 应该不是const的问题 构造函数中没有修改东西的~~
[/Quote]

下面是我测试代码,A a1,a2; 时编译通过, const A a1,a2;时报于楼住相同错误



class A
{
public:
A():i(10)
{

};
A operator =(A& a)
{
this->i = a.i;
return *this;
};
private:
int i;
};
//typedef int* iPTYPE;
void main(void)
{
A a1,a2;
a1 = a2;
}

Dinelgua 2009-02-17
  • 打赏
  • 举报
回复
因为你操作的是const对象
degree_37 2009-02-17
  • 打赏
  • 举报
回复
根据三楼的方法,已经解决了! 但是不知道原因...

为什么我写成

QVertex3d::QVertex3d(QVertex3d &vertex3d)

就不行呢

写成

QVertex3d(QVertex3d const &);

就可以...
waizqfor 2009-02-17
  • 打赏
  • 举报
回复

QVector3d QVector3d::operator =(QVector3d const &vector3d)
{
m_x = vector3d.m_x;
m_y = vector3d.m_y;
m_z = vector3d.m_z;
return *this;
}
QFace3d QFace3d::operator =(QFace3d const &face)
{
v0 = face.v0;
v1 = face.v1;
v2 = face.v2;
m_Normal = face.m_Normal;
}

试试
degree_37 2009-02-17
  • 打赏
  • 举报
回复
回1楼: 我去掉了const 还是有同样的错误 而且错误多了两个, 应该不是const的问题 构造函数中没有修改东西的~~
hhyttppd 2009-02-17
  • 打赏
  • 举报
回复

QVector3d operator =(QVector3d const &vector3d);

QVector3d(QVector3d const &);
Dinelgua 2009-02-17
  • 打赏
  • 举报
回复
class ''QVector3d' ' : no copy constructor available or copy constructor is declared

这是缺少拷贝构造函数
楼住加一个就行
QVector3d(const QVector3d&)


error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const QNormal'
此错误同迁移贴内答复 是const导致的,非const没有问题可以使用operator=
Dinelgua 2009-02-17
  • 打赏
  • 举报
回复
注意发生问题的是'const QVector3d'
是const类型 不能修改内容

如果是普通类型QVector3d 是没有问题的 俺测试过了

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧