Kamil Dajerling
Kamil Dajerling

Reputation: 13

Overloaded member function not found

I have a problem with my code, the error is:

    1>c:\users\grother\documents\obiektowe\lab05_195975\lab05_195975\czlowiek.cpp(6): error C2511: 'czlowiek::czlowiek(void)' : overloaded member function not found in 'czlowiek'
    1>c:\users\grother\documents\obiektowe\lab05_195975\lab05_195975\czlowiek.h(3) : see declaration of 'czlowiek'

This is czlowiek.h

class czlowiek      
{
    private:

    public: 
        int wiek, pola, r;
        char plec, *p, imie[15], nazwisko[25];
        static int n;
        string ulubioneKsiazki;

        //czlowiek();
        virtual ~czlowiek();
        czlowiek(const string& ulubioneKsiazki="Brak informacji")
        {
            this->ulubioneKsiazki=ulubioneKsiazki;
        };
};

and this is czlowiek.cpp:

#include "stdafx.h"
#include "czlowiek.h"

int czlowiek::n=0;

czlowiek::czlowiek():p(0)
{
    n++;
}

czlowiek::~czlowiek()
{
    n--;
}

I've tried changing the constructor, but I have no idea how to make this one work. Thanks in advance :)

Upvotes: 1

Views: 2773

Answers (1)

juanchopanza
juanchopanza

Reputation: 227420

You need to remove the definition of the default constructor from your .cpp file, since you have a single parameter constructor with a default parameter:

czlowiek(const string& ulubioneKsiazki="Brak informacji")
{
    this->ulubioneKsiazki=ulubioneKsiazki;
};

This acts as a default constructor, since it can be invoked without arguments.

Another alternative is to remove the default parameter in the single parameter constructor, and add a declaration for the default constructor. For example:

czlowiek() : ulubioneKsiazki="Brak informacji" {}
czlowiek(const string& ulubioneKsiazki) : ulubioneKsiazki(ulubioneKsiazki) {}

Bear in mind that your class has quite a few other data members that should probably be initialized.

Upvotes: 2

Related Questions