ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用默认复制构造函数的C vector :: push_back

2019-10-01 07:05:00  阅读:254  来源: 互联网

标签:c vector copy copy-constructor


我有一个类(Uniform),它有一个带2个参数的构造函数和一个默认的复制构造函数(它只包含int,float,std :: vector和std :: map).我创造了一个

std::vector<Uniform> uniforms

我想填写使用

uniforms.push_back()

线.我使用此代码来执行此操作(第二行仅用于测试复制构造函数,因为它当前失败)

Uniform uni(uniform_name,type);
Uniform uni2=uni;
uniforms.push_back(uni2);

默认构造函数工作正常,“uni2 = uni”编译没有问题(因此默认的复制构造函数也可以),但push_back返回(使用g作为编译器):

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.0/../../../../include/c++/4.6.0/ext/new_allocator.h:108:9: erreur: no matching function for call to ‘Uniform::Uniform(const Uniform&)’

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.0/../../../../include/c++/4.6.0/ext/new_allocator.h:108:9: note: candidates are:

./inc/uniform.h:16:5: note: Uniform::Uniform(std::string, Uniform_Type)

./inc/uniform.h:16:5: note: candidate expects 2 arguments, 1 provided

./inc/uniform.h:14:7: note: Uniform::Uniform(Uniform&)

./inc/uniform.h:14:7: note: no known conversion for argument 1 from ‘const Uniform’ to ‘Uniform&’

谢谢 :)

解决方法:

当你说“默认复制构造函数”(通常没什么意义)时,我假设你的意思是“隐式声明的复制构造函数”或“编译器提供的复制构造函数”

编译器提供的复制构造函数的确切签名将取决于Uniform类的内容.它可以是Uniform :: Uniform(const Uniform&)或Uniform :: Uniform(Uniform&),再次依赖于Uniform(你没有提供)的细节.

例如,如果你的Uniform包含一个T类型的子对象(base或成员),其复制构造函数被声明为T :: T(T&)(无const),那么Uniform的隐式构造函数也将被隐式声明为Uniform: :Uniform(Uniform&)(无const).

完整的规格可以在语言标准中找到(12.8 / 5)

The implicitly-declared copy
constructor for a class X will have
the form

X::X(const X&)

if

— each
direct or virtual base class B of X
has a copy constructor whose first
parameter is of type const B& or const
volatile B&, and

— for all the
nonstatic data members of X that are
of a class type M (or array thereof),
each such class type has a copy
constructor whose first parameter is
of type const M& or const volatile
M&.

Otherwise, the implicitly
declared copy constructor will have
the form

X::X(X&)

An
implicitly-declared copy constructor
is an inline public member of its
class.

push_back实现需要Uniform :: Uniform(const Uniform&),但是你的类中的某些东西会使它成为Uniform :: Uniform(Uniform&).因此错误.没有看到你的制服的定义就没有办法说出它是什么.

标签:c,vector,copy,copy-constructor
来源: https://codeday.me/bug/20191001/1837945.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有