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

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

问题描述

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

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

我想用

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);

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

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: 没有匹配函数调用'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: 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:注意:候选人是:

/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: 注意:Uniform::Uniform(std::string, Uniform_Type)

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

./inc/uniform.h:16:5:注意:候选人需要 2 个参数,提供 1 个

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

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

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

./inc/uniform.h:14:7: 注意:没有已知的参数 1 从‘const Uniform’到‘Uniform&’的转换

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

谢谢:)

推荐答案

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

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

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

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).

例如,如果您的 Uniform 包含 T 类型的子对象(基类或成员),其复制构造函数声明为 T::T(T&)(没有const),那么Uniform的隐式构造函数也会被隐式声明为Uniform::Uniform(Uniform &)(没有 const).

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).

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

A full specification can be found in the language standard (12.8/5)

隐式声明的副本类 X 的构造函数将具有表格

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

X::X(const X&)

如果

——每个X 的直接或虚拟基类 B有一个拷贝构造函数,它的第一个参数的类型为 const B&或常量易挥发的 B& 和

— 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

——对于所有X 的非静态数据成员是类类型 M(或其数组),每个这样的类类型都有一个副本第一个参数为的构造函数const M& 类型或 const 易失性并购.

— 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&).因此错误.如果没有看到你的 Uniform 的定义,就无法说出它是什么.

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 使用默认复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)