[英]C++ vector::push_back using default copy constructor
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).我有一个 class (Uniform),它有一个带有 2 个参数的构造函数和一个默认的复制构造函数(它只包含 int、float、一个 std::vector 和一个 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):默认构造函数工作正常,“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: 错误:没有匹配的 function 调用 '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: /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: 注意:Uniform::Uniform(std::string, Uniform_Type)
./inc/uniform.h:16:5: note: candidate expects 2 arguments, 1 provided ./inc/uniform.h:16:5:注意:候选人需要 2 个 arguments,提供 1 个
./inc/uniform.h:14:7: note: Uniform::Uniform(Uniform&) ./inc/uniform.h:14:7: 注意:Uniform::Uniform(Uniform&)
./inc/uniform.h:14:7: note: no known conversion for argument 1 from 'const Uniform' to 'Uniform&' ./inc/uniform.h:14:7:注意:没有已知的参数 1 从 'const Uniform' 到 '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.编译器提供的复制构造函数的确切签名将取决于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::Uniform(const Uniform &)
或Uniform::Uniform(Uniform &)
,这再次取决于Uniform
的详细信息(您没有提供)。
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
).例如,如果您的Uniform
包含T
类型的子对象(基类或成员),其复制构造函数声明为T::T(T &)
(无const
),则Uniform
的隐式构造函数也将隐式声明为Uniform::Uniform(Uniform &)
(无const
)。
A full specification can be found in the language standard (12.8/5)完整的规范可以在语言标准 (12.8/5) 中找到
The implicitly-declared copy constructor for a class X will have the form class X 的隐式声明的复制构造函数将具有以下形式
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 — X 的每个直接或虚拟基础 class B 都有一个复制构造函数,其第一个参数的类型为 const B& 或 const volatile B&,并且
— 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&. — 对于 X 的所有属于 class 类型 M(或其数组)的非静态数据成员,每个此类 class 类型都有一个复制构造函数,其第一个参数的类型为 const M& 或 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.隐式声明的复制构造函数是其 class 的内联公共成员。
The push_back
implementation needs Uniform::Uniform(const Uniform &)
, but something in your class causes it to be Uniform::Uniform(Uniform &)
. push_back
实现需要Uniform::Uniform(const Uniform &)
,但是您的 class 中的某些内容导致它是Uniform::Uniform(Uniform &)
。 Hence the error.因此错误。 There's no way to say what it is without seeing the definition of your Uniform
.如果没有看到Uniform
的定义,就无法说出它是什么。
Your copy constructor needs to take its argument as a const reference:您的复制构造函数需要将其参数作为const引用:
Uniform::Uniform(const Uniform& other)
Your copy constructor should accept const Uniform&
and not Uniform&
as the one you have does.您的复制构造函数应该像您所拥有的那样接受const Uniform&
而不是Uniform&
。
You failed to include the copy constructor (sic:!!) but you must have defined it wrongly:您未能包含复制构造函数(原文如此:!!),但您必须错误地定义它:
Uniform::Uniform(Uniform&)
{
....
}
should be (note the const )应该是(注意const )
Uniform::Uniform(const Uniform&)
{
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.