0

I have an assignment for class and i received a header file and need to implement the methods of the class. I'm having trouble with implementing the copy constructor and overloading the = operator. Here is some of the header file:

class MontaznaKuca
{
private:
    double kvadratura;
    string bojaFasade;
    string vlasnik;
    bool postojiVrt;
    Kuhinja* kuhinja;
    vector<Soba*> sobe; //Soba is a seperate class defined in another header 
public:
    MontaznaKuca(void);
    MontaznaKuca(double kvadratura, string bojaFasade, string imaVrt);
    MontaznaKuca(const MontaznaKuca& kuca);
    ~MontaznaKuca(void);

    MontaznaKuca& operator=(const MontaznaKuca& kuca);
...

My attempt ate the copy constructor and = operator overloading

MontaznaKuca::MontaznaKuca(const MontaznaKuca& kuca) : sobe(kuca.sobe.size())
{
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;
    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe[i] = new Soba(*kuca.sobe[i]);

}

MontaznaKuca& MontaznaKuca::operator=(const MontaznaKuca& kuca) : sobe(kuca.sobe.size())
{
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;
    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe[i] = new Soba(*kuca.sobe[i]);
    return *this;
}

I did this by studying some code on the web and visual studio gives me an error

: constructor initializer lists are only allowed on constructor definitions

Can someone explain to me how to do these two functions, because i am completely lost.

nixpix
  • 53
  • 1
  • 5

1 Answers1

0

The compiler has told you the error: constructor initializer lists are only allowed on constructor definitions.
You're implementing the copy constructor, so constructor initializer lists is not allowed, and it's only allowed on constructor while you try to use it on overloading the assignment operator. I'd recommend you to read these posts: Copy constructor and = operator overload in C++: is a common function possible? What is the copy-and-swap idiom?

MontaznaKuca::MontaznaKuca(const MontaznaKuca& kuca)
{
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;

    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe.push_back(new Soba(*kuca.sobe[i]) ); 
}

MontaznaKuca& MontaznaKuca::operator=(const MontaznaKuca& kuca)
{
    // copy-swap idiom is a better approach.
    kvadratura = kuca.kvadratura;
    bojaFasade = kuca.bojaFasade;
    vlasnik = kuca.vlasnik;
    postojiVrt = kuca.postojiVrt;
    delete kuhinja; //deallocate old memory
    kuhinja = new Kuhinja;
    kuhinja = kuca.kuhinja;
    // before allocating new memory you need deallocate old memory
    ...
    for (size_t i = 0; i < kuca.sobe.size(); ++i)
        sobe.push_back(new Soba(*kuca.sobe[i]) );
    return *this;
}
Community
  • 1
  • 1
jfly
  • 7,715
  • 3
  • 35
  • 65
  • Thank you so much. I finally realized what i was doing wrong when i saw your code. I read the posts you posted and learned a lot. Again thank you for the help. – nixpix Apr 11 '14 at 01:27