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

2022-01-20 00:00:00 复制 vector c++ copy-constructor

我有一个类(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.

相关文章