显式复制构造函数编译错误 [英] explicit copy constructor compile error

查看:103
本文介绍了显式复制构造函数编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中检查操作符重载,遇到了一些我没有想到的并且有一些疑问。



我的拷贝构造函数声明并实现为

 显式Vector(const Vector& v); 

Vector :: Vector(const Vector&v):
_x(v._x),_y(v._y),_z(v._z){}



然后我重载复合赋值运算符

  Vector Vector :: operator +(const Vector& v)const 
{
Vector tmp(* this);
tmp + = v;
return tmp;
}

Vector Vector :: operator-(const Vector& v)const
{
Vector tmp
tmp - = v;
return tmp;
}

但是,在 return 语句我得到一个错误说没有匹配的构造函数初始化'Vector'



我添加到我的构造函数是显式关键字,我删除它和代码编译很好,为什么?



我也在检查C ++ 11的新东西,发生了我可以声明我的构造函数像一个移动构造函数

 显式向量(const Vector& v); 

并且代码编译正常。如果我这样做,我必须有复制和移动构造函数?

 显式Vector(const Vector& v); 
explicit Vector(const Vector& v);

或者只是移动构造函数会正常工作?如果我想坚持C ++ 11,正确的方法是什么?

解决方案

,但函数

  Vector Vector :: operator +(const Vector& v)const 



  Vector Vector :: operator- (const Vector& v)const 

必须返回值, $ c> explicit (换句话说,它们不能将 tmp 复制到返回的对象中)。


$ b $我也是从C ++ 11检查新的东西,发生了,我可以声明我的构造函数像一个移动构造函数
显式Vector(const Vector& ;& v); ,代码编译正好。如果我这样做,我必须有复制和移动构造函数?


不知道我明白你的意思。如果你只声明一个显式的移动构造函数(这将阻止编译器生成一个默认的复制构造函数),你会有同样的问题。我不能生成一个可编译代码。


I was checking operator overloading in C++ and came across something I did not expect and have some doubts about it.

My copy constructor is declared and implemented as as

explicit Vector(const Vector& v);

Vector::Vector(const Vector& v) :
_x(v._x), _y(v._y), _z(v._z) {}

then I am overloading the compound assignment operators

Vector Vector::operator+(const Vector& v) const
{
    Vector tmp(*this);
    tmp += v;
    return tmp;
}

Vector Vector::operator-(const Vector& v) const
{
    Vector tmp(*this);
    tmp -= v;
    return tmp;
}

however, in the return statements I got an error saying no matching constructor for initialization of 'Vector'.

Since the only thing I added to my constructor was the explicit keyword, I deleted it and the code compiles just fine, why?

I also was checking new stuff from C++11 and occurred that I can declare my constructor like a moving-constructor

explicit Vector(const Vector&& v);

and the code compiles just fine. If I do that, do I have to have both copy and move constructors?

explicit Vector(const Vector& v);
explicit Vector(const Vector&& v);

or just having the move constructor will work fine? If I want to stick to C++11, what is the correct approach to follow?

解决方案

You defined an explicit copy constructor, but the functions

Vector Vector::operator+(const Vector& v) const

and

Vector Vector::operator-(const Vector& v) const

must return by value, and cannot anymore, due to explicit (in other words, they cannot copy tmp into the returned object).

I also was checking new stuff from C++11 and occurred that I can declare my constructor like a moving-constructor explicit Vector(const Vector&& v); and the code compiles just fine. If I do that, do I have to have both copy and move constructors?

Not sure I understand what you mean here. You will have the same issue if you only declare an explicit move constructor (which will prevent the compiler from generating a default copy constructor). I am not able to produce a "compilable" code.

这篇关于显式复制构造函数编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆