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

A

Afshin

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
 
V

Victor Bazarov

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
 
A

Afshin

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

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

A
 
V

Victor Bazarov

On 8/12/2011 8:24 AM, Afshin wrote:
[...]

V

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
 
A

Afshin

On 8/12/2011 8:24 AM, Afshin wrote:
 [...]
V
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

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 (){
 
V

Victor Bazarov

[..]
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
 
A

Afshin

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


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
 
V

Victor Bazarov

[..] 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top