-4

I try to copy the vector comanda to the vector comenzi :

 public :
        Ospatar(char nume[30],Comanda * comanda,int nrcom,char sex,int varst)
        {
            strcpy(Nume,nume);

            comenzi=new Comanda[sizeof(comanda)];
            for(int i=0;i<sizeof(comanda);i++)
            {
                comenzi[i]=comanda[i];

            }
            cout<<endl;

            nrComenzi=nrcom;
            gen=sex;
            varsta=varst;

        }

And here is the overloaded operator from Class Comanda :

Comanda& operator=(Comanda  c)
    {
        Prod.set_denProd(c.Prod.get_denProd());

        Num=c.Num;
        nrPortii=c.nrPortii;
        date.zi=c.date.zi;
        date.luna=c.date.luna;
        date.an=c.date.an;

    }

the first two elements of vector comanda are well transfered to vector comenzi but after that i get some random numbers ...

Oana
  • 9
  • 2

1 Answers1

0

Generally you should use std::vertor -- this is the right way to do that in C++. But assuming that you're not allowed to use standard library:

First your Comanda::operator= should take argument via reference (preferably reference to const):

Comanda& operator=(const Comanda& c)

Then, sizeof(comanda) returns size of pointer to Comanda, not number of elements in the array. You generally have to pass the size of the array in separate argument (I guess nrcom is the one).

So:

comenzi = new Comanda[nrcom];
for (int i=0; i<nrcom; i++)

etc...

You'll of course need to clean the code more (what's that strcpy thing), and last but not least don't forget to add delete [] for your allocated data.

seb
  • 326
  • 1
  • 5