void Boo::Set_Car(string this_car) {
Car += this_car;
}
---------------------------------------
I don't know why I can't do the "+=" operation on my private class
data. For example, the following code works fine until I try to
access the private data member:
-----------------------------------------
1. Class Boo {
2. private:
3. string Car;
4. public:
5. void Set_Car(string);
6. }
7.
8. void Boo::Set_Car(string this_car) {
9. string Car2;
10. Car2 += this_car; // this works fine
11. Car += this_car; // crashes here again
12. }
---------------------------------------
So if I declare a string, Car2, inside the function Set_Car(), it
works fine. But it crashes when I try to access the private data
member Car. I've tried using "Boo::Car += this_car;" in place of line
11. and it still crashes.
I am using Microsoft Visual C++ 6.0 edition.
I'm all out of ideas.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
You are invoking Set_Car on an invalid object (already deleted,
garbage memory, etc...). You need to search for the cause on
a higher level.
--
Dragan
Clearly I am missing something because I cannot see anything about a
deleted or invalid object in the above code. Indeed in the code quoted I
cannot see any object of type Boo.
--
Well, my mistake for sounding so arrogant. But, I'm 95% sure this is
what's happening. Car, as part of *this object is "damaged". Happens
to me when I rush things instead of implementing a proper memory
management. Can also happen when one misuses iterators.
I hope OP will inform us about the bug once he finds it. I need
something to feed my ego and arrogance... :-D
--
Dragan
{ quoted sig and banner removed. don't quote extraneous material. tia., -mod }
Class Boo doesn't have a constructor so that the object can't be
operated? On the other hand, Car2 is a variable generated locally,
i.e., on the heap, so you can use +=. Adding a simple ctor in Boo
works in VC.
class Boo {
private:
string Car;
public:
Boo(string Car);
void Set_Car(string);
};
Boo::Boo(string Car) : Car("whatever")
{}
void Boo::Set_Car(string this_car) {
Car += this_car; // ok now
}
--
Of course class Boo has a ctor, in fact it has two, the default ctor and
the copy ctor. Until C++0x it is not possible to write a class without
at least one plain ctor (that can suppress compiler generation of a
default ctor) and a copy ctor (either user written of compiler generated)
C++0x has syntax to declare that a function is not defined and so
generate compile time errors if an attempt is made to call it. This
syntax is useful in many places including inhibiting unwanted
conversions in the presence of an overload set:
void foo(int);
void foo(long) = delete;
int main(){
int i(0);
long el(0);
foo(i); // OK
foo(el); // error