Re: Problem in calling an overloaded operator= from inside another
overloaded operator=
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::operator< (const cProduct& rhs);
~cProduct();
};
cResource& cResource::operator= (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[i]; // 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