C ++ vector :: push_back使用默认的拷贝构造函数 [英] C++ vector::push_back using default copy constructor

查看:1451
本文介绍了C ++ vector :: push_back使用默认的拷贝构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  std :: vector< Uniform>制服

pre> uniforms.push_back()

我使用这个代码来做这个(第二行就是这里测试的复制构造函数,因为它目前失败)

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

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


/ usr / lib / gcc / x86_64- linux-gnu / 4.6.0 /../../../../ include / c ++ / 4.6.0 / ext / new_allocator.h:108:9:erreur:没有匹配函数调用'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:注意:候选人是:



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



./ inc / uniform.h:16:5: ,提供1个



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



./ inc / uniform.h:14:7:注意:没有从'const Uniform'到'Uniform&'


的参数1的已知转换

感谢:)

解决方案

当你说default copy constructor ),我假设你的意思是隐式声明的复制构造函数或编译器提供的复制构造函数



编译器提供的复制构造函数的确切签名将取决于类 Uniform 的内容。它可以是 Uniform :: Uniform(const Uniform&) Uniform :: Uniform(Uniform&) depends ,再次对 Uniform (您没有提供)的详细信息。



例如,如果您的 Uniform 包含 T 类型的子对象(基础或成员),其副本构造函数声明为 T :: T(T&)(no const ),然后 Uniform 隐式构造函数也将被隐式声明为 Uniform :: Uniform(Uniform&)(no const )。



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


对于类X,隐式声明的拷贝
构造函数将有
形式



X :: X const X&)



if



- 每个
X
的虚拟基类B具有一个拷贝构造函数,其第一个
参数是const B&或const
volatile B&和



- 对于所有
X的非静态数据成员是
类类型M (或其数组),
每个这样的类类型都有一个副本
构造函数,其第一个参数是
类型const M&或const volatile
M&。



否则,隐式的
声明的拷贝构造函数将有
形式



X :: X(X&)



An
隐式声明的复制构造函数
是其
类的内联公共成员。


push_back 实现需要 Uniform :: Uniform(const Uniform&),但是类中的某些东西会导致 Uniform :: Uniform(Uniform&)。因此错误。没有办法说明没有看到你的 Uniform 的定义。


I have a class (Uniform) that has a constructor with 2 parameters, and a default copy constructor (it only contains int, floats, a std::vector and a std::map). I created a

std::vector<Uniform> uniforms

that I want to fill using the

uniforms.push_back()

line. I use this code to do that (the 2nd line is just here to test the copy constructor, as it currently fails)

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

The default constructor works fine, the "uni2=uni" compiles without problem (so the default copy constructor is OK too), but the push_back returns (using g++ as a compiler):

/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&’

Thanks :)

解决方案

When you say "default copy constructor" (which generally makes little sense), I assume you mean "implicitly-declared copy constructor" or "compiler-provided copy constructor"

The exact signature of the compiler-provided copy constructor will depend on the contents of your Uniform class. It could be Uniform::Uniform(const Uniform &) or Uniform::Uniform(Uniform &) depending, again, on the details of Uniform (which you didn't provide).

For example, if your Uniform includes a subobject (base or member) of type T, whose copy constructor is declared as T::T(T &) (no const), then Uniform's implicit constructor will also be implicitly declared as Uniform::Uniform(Uniform &) (no const).

A full specification can be found in the language standard (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.

The push_back implementation needs Uniform::Uniform(const Uniform &), but something in your class causes it to be Uniform::Uniform(Uniform &). Hence the error. There's no way to say what it is without seeing the definition of your Uniform.

这篇关于C ++ vector :: push_back使用默认的拷贝构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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