When a C++ class 'class1' contains a vector (array) of class 'class2', and you want to construct an object of type 'class1' from another object of type 'class 1', you must use this constructor:
class2::class2(class2&)
so that the elements of the vector can be constructed.
The constructor, called a copy constructor, takes just one parameter (which is a reference to its class).
Usually, the compiler supplies a copy constructor automatically.
However, if you have defined a constructor for class 'class2' that has a parameter of type 'class2&' and has additional parameters with default values, the copy constructor can't exist and can't be created by the compiler.
This is because these two can't be distinguished:
class2::class2(class2&)
class2::class2(class2&, int = 1)
You must redefine this constructor so that not all parameters have default values.
You can then define a reference constructor or let the compiler create one.
Cannot find class::operator= ...
When a C++ class 'class1' contains a vector (array) of class 'class2', and you want to copy a class of type 'class1', you must use this assignment operator:
class2::class2(class2&)
so that the elements of the vector can be copied.
Usually, the compiler automatically supplies this operator.
However, if you have defined an operator= for class 'class2' that does not take a parameter of type 'class2&,' the compiler will not supply it automatically--you must supply one.
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|