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 🙂
Best Answer
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 beUniform::Uniform(const Uniform &)
orUniform::Uniform(Uniform &)
depending, again, on the details ofUniform
(which you didn't provide).For example, if your
Uniform
includes a subobject (base or member) of typeT
, whose copy constructor is declared asT::T(T &)
(noconst
), thenUniform
's implicit constructor will also be implicitly declared asUniform::Uniform(Uniform &)
(noconst
).A full specification can be found in the language standard (12.8/5)
The
push_back
implementation needsUniform::Uniform(const Uniform &)
, but something in your class causes it to beUniform::Uniform(Uniform &)
. Hence the error. There's no way to say what it is without seeing the definition of yourUniform
.