Board index » cppbuilder » "Class with properties cannot be copied by value"

"Class with properties cannot be copied by value"


2005-02-23 02:56:31 PM
cppbuilder26
I am receiving this error within a system header file, but I'm not sure what
part of my code is generating the error. I've marked the line that is
throwing it. Again, this is not my code, it is something within
_algobase.h. Any idea of what I've written that could be the problem?
Something with vectors maybe?
template <class _RandomAccessIter, class _OutputIter, class _Distance>
inline _OutputIter
__copy(_RandomAccessIter __first, _RandomAccessIter __last,
_OutputIter __result, const random_access_iterator_tag &, _Distance*)
{
for (_Distance __n = __last - __first; __n>0; --__n) {
*__result = *__first; <<<<=== ERROR HERE!
++__first;
++__result;
}
return __result;
}
 
 

Re:"Class with properties cannot be copied by value"

I have noticed two classes within my application (one derived from TImage
and one derived from TGraphicControl) that have __property variables.
Unfortunately, simply removing these would be quite a big issue. Judging by
the error message, I can't think of anything else that would be causing this
problem, however. Any ideas?
 

Re:"Class with properties cannot be copied by value"

On Wed, 23 Feb 2005 01:01:07 -0600, "Blake Young" < XXXX@XXXXX.COM >
wrote:
Quote
I have noticed two classes within my application (one derived from TImage
and one derived from TGraphicControl) that have __property variables.
Unfortunately, simply removing these would be quite a big issue. Judging by
the error message, I can't think of anything else that would be causing this
problem, however. Any ideas?

You have to provide the operator= (and, possibly, also the copy constructor)
because BCC32 doesn't create automatically the default operator= when the
class has properties.
Giuliano
 

{smallsort}

Re:"Class with properties cannot be copied by value"

Quote
You have to provide the operator= (and, possibly, also the copy
constructor)
because BCC32 doesn't create automatically the default operator= when the
class has properties.
Would it simply look something like this?
class MyClass
{
private:
int FValue;
int GetValue() { return FValue; }
void SetValue(int newV) { FValue = newV; }
public:
MyClass& operator=(const MyClass &);
MyClass(const MyClass &);
bool trueOrFalse;
__property int Value = { read = GetValue; write = SetValue };
};
MyClass& MyClass::operator=(const MyClass &incoming)
{
FValue = incoming.Value;
trueOrFalse = incoming.trueOrFalse;
return *this;
}
MyClass::MyClass(const MyClass &incoming)
{
FValue = incoming.Value;
trueOrFalse = incoming.trueOrFalse;
}
This is only from reading some tutorials via Google. I have never actually
written functions to overload operators or copy constructors. Does it look
OK?
Thanks
 

Re:"Class with properties cannot be copied by value"

"Blake Young" < XXXX@XXXXX.COM >wrote in message
Quote
I am receiving this error within a system header file, but I'm not
sure what part of my code is generating the error. I've marked
the line that is throwing it. Again, this is not my code, it is
something within _algobase.h. Any idea of what I've written
that could be the problem?
You are using an STL container which contains actual object instances,
rather than pointers to instances, of a class type that has at least 1
property. In other words:
class Test
{
//...
public:
__property Type Value = {...};
};
std::vector<Test*>vTest1; // <-- using pointers, OK
std::vector<Test>vTest2; // <-- not using pointers!
Elements stored in STL containers must be copyable in order for various STL
operations to work properly. By default, classes with properties do not
have a suitable '=' operator implemented.
Gambit
 

Re:"Class with properties cannot be copied by value"

Quote
You are using an STL container which contains actual object instances,
rather than pointers to instances, of a class type that has at least 1
property. In other words:

class Test
{
//...
public:
__property Type Value = {...};
};

std::vector<Test*>vTest1; // <-- using pointers, OK
std::vector<Test>vTest2; // <-- not using pointers!

Elements stored in STL containers must be copyable in order for various
STL
operations to work properly. By default, classes with properties do not
have a suitable '=' operator implemented.
Indeed I am doing what you said in many parts of my code. That is,
std::vector<Test>vTest2; // <-- not using pointers!
Does the overridden operator= and copy constructor that I posted look like
it will solve my problem? (The post is somewhere in this thread.)
Thanks
 

Re:"Class with properties cannot be copied by value"

"Blake Young" < XXXX@XXXXX.COM >wrote in message
Quote
Does the overridden operator= and copy constructor that I
posted look like it will solve my problem?
The class will still have a property present even if you do implement the
assignment operator and copy constructor. Classes with properties cannot
usually be copied by value. However, I cannot reproduce the error to begin
with, so can you please show the actual code you are using that is causing
the error?
Gambit
 

Re:"Class with properties cannot be copied by value"

On Wed, 23 Feb 2005 11:32:35 -0600, "Blake Young" < XXXX@XXXXX.COM >
wrote:
Quote
Would it simply look something like this?

class MyClass
{
private:
int FValue;

int GetValue() { return FValue; }
void SetValue(int newV) { FValue = newV; }

public:
MyClass& operator=(const MyClass &);
MyClass(const MyClass &);

bool trueOrFalse;

__property int Value = { read = GetValue; write = SetValue };
};

MyClass& MyClass::operator=(const MyClass &incoming)
{
FValue = incoming.Value;
trueOrFalse = incoming.trueOrFalse;

return *this;
}

MyClass::MyClass(const MyClass &incoming)
{
FValue = incoming.Value;
trueOrFalse = incoming.trueOrFalse;
}


This is only from reading some tutorials via Google. I have never actually
written functions to overload operators or copy constructors. Does it look
OK?
The assignment operator is pretty correct; however, for sake of optimization,
it's better to check for self assignment. Moreover you can use the private
member directly instead of the property to copy FValue.
MyClass& MyClass::operator=(const MyClass &incoming)
{
if ( &incoming != this ) {
FValue = incoming.FValue;
trueOrFalse = incoming.trueOrFalse;
}
return *this;
}
Quote
__property int Value = { read = GetValue; write = SetValue };
Seems you have used a semicolon instead of comma to separate the getter from
the setter. You would have had to write:
__property int Value = { read = GetValue, write = SetValue };
Finally, IMHO you would have had to write the copy constructor using
initializer list:
MyClass::MyClass(const MyClass &incoming)
: FValue( incoming.FValue ), trueOrFalse( incoming.trueOrFalse )
{
}
Regards
Giuliano
 

Re:"Class with properties cannot be copied by value"

Quote
However, I cannot reproduce the error to begin
with, so can you please show the actual code you are using that is causing
the error?
I honestly wish I could. The problem is that I have MANY cases where I've
created vectors of non-pointer instances. The second problem is that all of
these were added at the same time before I tried to compile again, so I am
not sure which one is exactly causing the problem. Like I said, the error
is being generated in the _algobase.h header, which isn't technically part
of my code. Unforunately, I'm starting to think that the best solution may
just be to remove all of the __property members from all of my classes
(there aren't that many I suppose) and see if that fixes it.
 

Re:"Class with properties cannot be copied by value"

Oh, just out of curiosity, what if the classes with property values are
derived from other classes with property values? For example, one of my
classes is TEMImage, which is derived from TImage. Therefore, it looks like
the following:
class TEMImage : public TImage
{
private:
int FViewTop;
...
int GetViewTop() { return FViewTop; }
void SetViewTop(int newV) { FViewTop = newV; }
public:
__property int ViewTop = { read = GetViewTop, write = SetViewTop };
};
It just so happens that my program has some vectors of the type:
vector<TEMImage>images;
In this case, if I were to get rid of the property ViewTop, would I still
receive errors since TImage also has property values like
Picture->Bitmap->Width and whatnot (and therefore TEMImage would have those
properties also)? So if that's the case, then simply removing my
user-created __property values might not fix anything. Right?
 

Re:"Class with properties cannot be copied by value"

"Blake Young" < XXXX@XXXXX.COM >wrote in message
Quote
I honestly wish I could. The problem is that I have MANY
cases where I've created vectors of non-pointer instances.
Just produce a single code example that demonstrates the error.
Gambit
 

Re:"Class with properties cannot be copied by value"

"Blake Young" < XXXX@XXXXX.COM >wrote in message
Quote
one of my classes is TEMImage, which is derived from TImage.
TImage is a VCL class. VCL objects cannot be created on the stack. They
must always be created dynamically on the heap.
Quote
vector<TEMImage>images;
Since TEMImage won't be able to be created on the stack anyway, you should
make your vector store pointers instead.
Quote
In this case, if I were to get rid of the property ViewTop, would
I still receive errors since TImage also has property values
Yes. Properties are still properties, regardless of class hierarchy.
Gambit