Problem in calling an overloaded operator= from inside anotheroverloaded operator=

Discussion in 'C++' started by Afshin, Aug 12, 2011.

  1. Afshin

    Afshin Guest

    Hi everybody,

    I am developing code for a scheduling problem in which I have a class
    for products (called cProduct) which contains, among others, an array
    of class cResource to represent the resources used to process the
    product. For both classes (cProduct and cResource), I have defined
    overloaded operator= ; with operator= for cResource being called from
    inside the overloaded operator= defined for cProduct. The problem is
    that in calling the internal operator= (from inside the opertor= for
    class cProduct) the value of rhs (being passed from internal call to
    operator=) seems to get lost!. I appreciate any hint or clue on how to
    fix this problem. Below is a summary of relevant classes and
    overloaded operator=:

    class cResource{
    public:
    cResource(); //constructor
    int x; //as a sample member variable
     
    Afshin, Aug 12, 2011
    #1
    1. Advertising

  2. On 8/12/2011 8:24 AM, Afshin wrote:
    > I am developing code for a scheduling problem in which I have a class
    > for products (called cProduct) which contains, among others, an array
    > of class cResource to represent the resources used to process the
    > product. For both classes (cProduct and cResource), I have defined
    > overloaded operator= ; with operator= for cResource being called from
    > inside the overloaded operator= defined for cProduct. The problem is
    > that in calling the internal operator= (from inside the opertor= for
    > class cProduct) the value of rhs (being passed from internal call to
    > operator=) seems to get lost!. I appreciate any hint or clue on how to
    > fix this problem. Below is a summary of relevant classes and
    > overloaded operator=:
    >
    > class cResource{
    > public:
    > cResource(); //constructor
    > int x; //as a sample member variable
    > .
    > cResource& operator= (const cResource& rhs);
    > .
    >
    > };
    >
    > cResource& cResource::eek:perator= (const cResource& rhs){
    > .
    > x = rhs.x;
    > .
    > return *this;
    >
    > }
    >
    > class cProduct{
    > public:
    > cProduct(int ResNum);
    > int y; // as a sample member variable
    > int TotalRes;
    > .
    > cResource *Resource; //the array of resources to process the
    > product
    > .
    > cProduct& operator= (const cProduct& rhs);
    >
    > };
    >
    > cProduct& cProduct::eek:perator= (const cProduct& rhs){
    > y = rhs.y;
    > for (int j=0; j<TotalRes; j++)
    > Resource[j] = rhs.Resource[j]; // HERE IS WHERE THE
    > PROBLEM OCCURS.
    > //The overloaded operator= for
    > Resource passes an 'undefined value' for rhs
    > .
    > return *this;
    > }
    >
    > Thanks in advance for your comments and suggestions!


    First suggestion: get yourself some patience (ask the neighbor or a
    friend, they might have some to spare). Three identical posts within
    half an hour is not a good sign.

    Second suggestion: read about "the Rule of Three". Apparently you're
    trying to manage dynamic memory in your 'cProduct' class and failing to
    do so correctly. The problem is *not* in your 'cProduct::eek:perator=',
    the problem is *not* in your 'cResource::eek:perator='. It's somewhere
    else. The 'cProduct' that arrives into 'cProduct::eek:perator=' is already
    screwed up by some earlier action (or inaction). You need to debug your
    program to understand what you do wrong or don't do that is needed.
    And, again, learn about "the Rule of Three" and about patience.

    Did I mention patience?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 12, 2011
    #2
    1. Advertising

  3. Afshin

    Afshin Guest

    On Aug 12, 1:53 pm, Victor Bazarov <> wrote:
    > On 8/12/2011 8:24 AM, Afshin wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > I am developing code for a scheduling problem in which I have a class
    > > for products (called cProduct) which contains, among others, an array
    > > of class cResource to represent the resources used to process the
    > > product. For both classes (cProduct and cResource), I have defined
    > > overloaded operator= ; with operator= for cResource being called from
    > > inside the overloaded operator= defined for cProduct. The problem is
    > > that in calling the internal operator= (from inside the opertor= for
    > > class cProduct) the value of rhs (being passed from internal call to
    > > operator=) seems to get lost!. I appreciate any hint or clue on how to
    > > fix this problem. Below is a summary of relevant classes and
    > > overloaded operator=:

    >
    > > class cResource{
    > > public:
    > >          cResource(); //constructor
    > >          int x; //as a sample member variable
    > >          .
    > >          cResource&  operator= (const cResource&  rhs);
    > >          .

    >
    > > };

    >
    > > cResource&  cResource::eek:perator= (const cResource&  rhs){
    > >          .
    > >          x = rhs.x;
    > >          .
    > >          return *this;

    >
    > > }

    >
    > > class cProduct{
    > > public:
    > >          cProduct(int ResNum);
    > >          int y; // as a sample member variable
    > >          int TotalRes;
    > >          .
    > >          cResource *Resource; //the array of resources to process the
    > > product
    > >          .
    > >          cProduct&  operator= (const cProduct&  rhs);

    >
    > > };

    >
    > > cProduct&  cProduct::eek:perator= (const cProduct&  rhs){
    > >           y = rhs.y;
    > >          for (int j=0; j<TotalRes; j++)
    > >                  Resource[j] = rhs.Resource[j]; // HERE IS WHERE THE
    > > PROBLEM OCCURS.
    > >                                      //The overloaded operator= for
    > > Resource passes an 'undefined value' for rhs
    > >          .
    > >          return *this;
    > > }

    >
    > > Thanks in advance for your comments and suggestions!

    >
    > First suggestion: get yourself some patience (ask the neighbor or a
    > friend, they might have some to spare).  Three identical posts within
    > half an hour is not a good sign.
    >
    > Second suggestion: read about "the Rule of Three".  Apparently you're
    > trying to manage dynamic memory in your 'cProduct' class and failing to
    > do so correctly.  The problem is *not* in your 'cProduct::eek:perator=',
    > the problem is *not* in your 'cResource::eek:perator='.  It's somewhere
    > else.  The 'cProduct' that arrives into 'cProduct::eek:perator=' is already
    > screwed up by some earlier action (or inaction).  You need to debug your
    > program to understand what you do wrong or don't do that is needed.
    > And, again, learn about "the Rule of Three" and about patience.
    >
    > Did I mention patience?
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Your response looks more phylosophical than technical! I appreciate
    though comments on where you think the problem is *not*.

    A
     
    Afshin, Aug 12, 2011
    #3
  4. On 8/12/2011 9:30 AM, Afshin wrote:
    > On Aug 12, 1:53 pm, Victor Bazarov<> wrote:
    >> On 8/12/2011 8:24 AM, Afshin wrote:
    >> [...]
    >>
    >> V
    >> --
    >> I do not respond to top-posted replies, please don't ask

    >
    > Your response looks more phylosophical than technical! I appreciate
    > though comments on where you think the problem is *not*.


    Well, I didn't have much to go on, did I? You didn't post your *entire*
    code that exhibits the problem, did you? If you did, I'd point out that
    you're either missing a copy constructor in 'cProduct' or something like
    that. Besides, if you're a beginner, don't use *naked pointers*. Use
    std::vector to keep your 'cResource' in 'cProduct'. What C++ book are
    you reading?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 12, 2011
    #4
  5. Afshin

    Afshin Guest

    On Aug 12, 2:39 pm, Victor Bazarov <> wrote:
    > On 8/12/2011 9:30 AM, Afshin wrote:
    >
    > > On Aug 12, 1:53 pm, Victor Bazarov<>  wrote:
    > >> On 8/12/2011 8:24 AM, Afshin wrote:
    > >>  [...]

    >
    > >> V
    > >> --
    > >> I do not respond to top-posted replies, please don't ask

    >
    > > Your response looks more phylosophical than technical! I appreciate
    > > though comments on where you think the problem is *not*.

    >
    > Well, I didn't have much to go on, did I?  You didn't post your *entire*
    > code that exhibits the problem, did you?  If you did, I'd point out that
    > you're either missing a copy constructor in 'cProduct' or something like
    > that.  Besides, if you're a beginner, don't use *naked pointers*.  Use
    > std::vector to keep your 'cResource' in 'cProduct'.  What C++ book are
    > you reading?
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Thanks Victor for your swift, precise and encouraging responses (I
    have already made some progress in 'the Rule of Three' didn't I?).
    Below are complete list of cProduct and cResource classes and the
    segment of main function where the problem exhibits itself:

    class cProduct{
    public:
    cProduct(int ResNum);
    char Code[CODE_SIZE];
    int IndexInProblemProductArray;
    int TotalRes;
    float *ProcessTime;
    int MarketDemand;
    float ContributionMargin;
    int MPS;
    float Throughput;
    float R_ratio; // CM/(ti,BN1)
    cResource *Resource;

    cProduct& operator= (const cProduct& rhs);
    bool cProduct::eek:perator< (const cProduct& rhs);
    ~cProduct();
    };

    cResource& cResource::eek:perator= (const cResource& rhs){
    int k;
    for (k=0; k<CODE_SIZE; k++)
    Code[k] = rhs.Code[k];
    IndexInProblemResourceArray = rhs.IndexInProblemResourceArray;
    Capacity = rhs.Capacity;
    UsedCapacity = rhs.UsedCapacity;
    LeftCapacity = rhs.LeftCapacity;
    CapacityDifference = rhs.CapacityDifference;
    DegreeOfCriticality = rhs.DegreeOfCriticality;
    Priority = rhs.Priority;

    return *this;
    }

    void main (){
     
    Afshin, Aug 12, 2011
    #5
  6. On 8/12/2011 10:12 AM, Afshin wrote:
    > [..]
    > Below are complete list of cProduct and cResource classes and the

    ^^^^^^^^^^^^^
    WTF is a "complete list"? I can't copy it and paste it into my editor
    to compile it and test it, can I?

    > segment of main function where the problem exhibits itself:
    >
    > class cProduct{
    > public:
    > cProduct(int ResNum);


    What does that do? My crystal ball is not working today...

    > char Code[CODE_SIZE];
    > int IndexInProblemProductArray;
    > int TotalRes;
    > float *ProcessTime;
    > int MarketDemand;
    > float ContributionMargin;
    > int MPS;
    > float Throughput;
    > float R_ratio; // CM/(ti,BN1)
    > cResource *Resource;
    >
    > cProduct& operator= (const cProduct& rhs);
    > bool cProduct::eek:perator< (const cProduct& rhs);
    > ~cProduct();
    > };
    >
    > cResource& cResource::eek:perator= (const cResource& rhs){
    > int k;
    > for (k=0; k<CODE_SIZE; k++)
    > Code[k] = rhs.Code[k];


    Ugh!...

    std::copy(rhs.Code, rhs.Code + CODE_SIZE, Code);

    > IndexInProblemResourceArray = rhs.IndexInProblemResourceArray;
    > Capacity = rhs.Capacity;
    > UsedCapacity = rhs.UsedCapacity;
    > LeftCapacity = rhs.LeftCapacity;
    > CapacityDifference = rhs.CapacityDifference;
    > DegreeOfCriticality = rhs.DegreeOfCriticality;
    > Priority = rhs.Priority;


    And, from the looks of it, you really *don't need this operator*, you
    can rely on the one that the compiler will provide for you.

    >
    > return *this;
    > }
    >
    > void main (){


    int main(){

    > .
    > .
    > for (i=0; i<pCP->ProdNum; i++){


    What's 'pCP'? How is it defined/declared/initialized?

    > cProduct *pTempProd = new cProduct(pCP->ResNum);
    > *pTempProd = pCP->Product; // THE PROBLEM HAPPENS HERE
    > ProductList.push_back(*pTempProd);
    > }
    > }
    >
    >
    > In an earlier version, I had defined 'list<cResource> Resource' in
    > cProduct and then changed it to 'cResource* Resource' as I thought the
    > later could be less problematic.


    You were mistaken, apparently, wouldn't you say? Change it to

    std::vector<cResource> Resource;

    and if you just let go of your desire to control every tiny bit of your
    program's behaviour, and start using standard containers where you need
    to store some items, ordered or sorted, unique, whatever, then you won't
    even need to define those problematic things like the copy constructors
    and the assignment operators.

    > Incidentally, I have some experience
    > of coding in C++ (although not continuously) including working with
    > dynamic memories. This however is the first time I needed to embed a
    > dynamic array within another dynamic array.


    Well, don't. I can't see any reason to not use 'std::vector'. Can you
    prove that you actually "need" to "embed a dynamic array within another
    dynamic array"?

    And start reading the FAQ, please. Make sure you get to FAQ 5.8 before
    posting another message.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 12, 2011
    #6
  7. Afshin

    Afshin Guest

    On Aug 12, 3:49 pm, Victor Bazarov <> wrote:
    > On 8/12/2011 10:12 AM, Afshin wrote:> [..]
    > > Below are complete list of cProduct and cResource classes and the

    >
    >              ^^^^^^^^^^^^^
    > WTF is a "complete list"?  I can't copy it and paste it into my editor
    > to compile it and test it, can I?
    >
    > > segment of main function where the problem exhibits itself:

    >
    > > class cProduct{
    > > public:
    > >    cProduct(int ResNum);

    >
    > What does that do?  My crystal ball is not working today...
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > >    char Code[CODE_SIZE];
    > >    int IndexInProblemProductArray;
    > >    int TotalRes;
    > >    float *ProcessTime;
    > >    int MarketDemand;
    > >    float ContributionMargin;
    > >    int MPS;
    > >    float Throughput;
    > >    float R_ratio; // CM/(ti,BN1)
    > >    cResource *Resource;

    >
    > >    cProduct&  operator= (const cProduct&  rhs);
    > >    bool cProduct::eek:perator<  (const cProduct&  rhs);
    > >    ~cProduct();
    > > };

    >
    > > cResource&  cResource::eek:perator= (const cResource&  rhs){
    > >    int k;
    > >    for (k=0; k<CODE_SIZE; k++)
    > >            Code[k] = rhs.Code[k];

    >
    > Ugh!...
    >
    >          std::copy(rhs.Code, rhs.Code + CODE_SIZE, Code);
    >
    > >    IndexInProblemResourceArray = rhs.IndexInProblemResourceArray;
    > >    Capacity = rhs.Capacity;
    > >    UsedCapacity = rhs.UsedCapacity;
    > >    LeftCapacity = rhs.LeftCapacity;
    > >    CapacityDifference = rhs.CapacityDifference;
    > >    DegreeOfCriticality = rhs.DegreeOfCriticality;
    > >    Priority = rhs.Priority;

    >
    > And, from the looks of it, you really *don't need this operator*, you
    > can rely on the one that the compiler will provide for you.
    >
    >
    >
    > >    return *this;
    > > }

    >
    > > void main (){

    >
    > int main(){
    >
    > >     .
    > >     .
    > >     for (i=0; i<pCP->ProdNum; i++){

    >
    > What's 'pCP'?  How is it defined/declared/initialized?
    >
    > >            cProduct *pTempProd = new cProduct(pCP->ResNum);
    > >            *pTempProd = pCP->Product; // THE PROBLEM HAPPENS HERE
    > >            ProductList.push_back(*pTempProd);
    > >     }
    > > }

    >
    > > In an earlier version, I had defined 'list<cResource>  Resource' in
    > > cProduct and then changed it to 'cResource* Resource' as I thought the
    > > later could be less problematic.

    >
    > You were mistaken, apparently, wouldn't you say?  Change it to
    >
    >     std::vector<cResource> Resource;
    >
    > and if you just let go of your desire to control every tiny bit of your
    > program's behaviour, and start using standard containers where you need
    > to store some items, ordered or sorted, unique, whatever, then you won't
    > even need to define those problematic things like the copy constructors
    > and the assignment operators.
    >
    >  > Incidentally, I have some experience
    >
    > > of coding in C++ (although not continuously) including working with
    > > dynamic memories. This however is the first time I needed to embed a
    > > dynamic array within another dynamic array.

    >
    > Well, don't.  I can't see any reason to not use 'std::vector'.  Can you
    > prove that you actually "need" to "embed a dynamic array within another
    > dynamic array"?
    >
    > And start reading the FAQ, please.  Make sure you get to FAQ 5.8 before
    > posting another message.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Thanks Victor, vector solved part of my problem. I'm now working on
    other issues. Back to your earlier question, I use "C++ Unleashed" and
    Visual C++ online reference pages as the main sources of help. I would
    warmly welcome alternative ideas. BTW, where do I find FAQs?

    A
     
    Afshin, Aug 16, 2011
    #7
  8. On 8/16/2011 6:03 AM, Afshin wrote:
    > [..] I use "C++ Unleashed" and
    > Visual C++ online reference pages as the main sources of help. I would
    > warmly welcome alternative ideas. BTW, where do I find FAQs?


    There are probably other places on the 'net where some FAQ are, but we
    usually refer here to this: http://www.parashift.com/c -faq-lite/

    As for the ideas for the sources of help, the compiler reference is good
    enough for a quick lookup of the interfaces and limitations, but don't
    use it as the source for learning the idioms and techniques. I would
    recommend "Accelerated C++" by Koenig and Moo, and there are plenty of
    other highly recommended books. See if you can find any suitable for
    you on http://accu.org/ (click on Book Reviews and search for C++, then
    read the actual reviews).

    Good luck!

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 16, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matt p
    Replies:
    3
    Views:
    474
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Nov 7, 2004
  2. Vinu
    Replies:
    6
    Views:
    471
    Jonathan Mcdougall
    Jul 12, 2005
  3. John Goche
    Replies:
    2
    Views:
    348
    Frederick Gotham
    Sep 4, 2006
  4. Afshin
    Replies:
    0
    Views:
    294
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    0
    Views:
    300
    Afshin
    Aug 12, 2011
Loading...

Share This Page