Reputation: 31
I have a default class Barista
which I want as the default, and then I have derived types of Barista
s to go along with it.
class Barista
{
public:
Barista();
virtual void getOrder();
virtual void intro(){cout << "";}
};
class JuniorBarista: public Barista
{
public:
JuniorBarista();
void getOrder() override;
void intro(){cout << "Hello my name is" << name_ << "What can I get you?" << endl;};
private:
string name_ = "Joe";
};
class SeniorBarista: public Barista
{
public:
SeniorBarista();
void getOrder() override;
void intro(){cout << "Hello my name is" << name_ << "What can I get you?" << endl;};
private:
string name_ = "jan";
};
class ManagerBarista: public Barista
{
public:
ManagerBarista();
void getOrder() override;
void intro(){cout << "Hello my name is" << name_ << "What can I get you?" << endl;};
private:
string name_ = "jon";
};
I'm trying to call the intro()
method of the JuniorBarista
in main()
and I am getting an "undefined reference" error.
I'm trying to call my function like this:
Barista *b;
b = new Barista();
b = new JuniorBarista();
b->intro();
I'm unsure of what exactly I'm doing wrong and would love an explanation why I am getting this error.
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Jaden\AppData\Local\Temp\ccUPD7MN.o:lab11p1.cpp:(.text+0x20): undefined reference to `Barista::Barista()'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Jaden\AppData\Local\Temp\ccUPD7MN.o:lab11p1.cpp:(.text+0x39): undefined reference to `JuniorBarista::JuniorBarista()'
collect2.exe: error: ld returned 1 exit status
Upvotes: 0
Views: 38
Reputation: 596582
You are declaring non-default, non-inline constructors in your classes, but where are the implementations for those constructors? The linker can't find them, hence the "unresolved" errors. If you are not going to actually implement the constructors, declare them as default
, eg:
class Barista
{
public:
Barista() = default;
...
};
class JuniorBarista: public Barista
{
public:
JuniorBarista() = default;
...
};
class SeniorBarista: public Barista
{
public:
SeniorBarista() = default;
...
};
class ManagerBarista: public Barista
{
public:
ManagerBarista() = default;
...
};
Same with getOrder()
, too. Each of your classes has a non-inline getOrder()
declared, but where are the implementations?
Also, you have a memory leak. You are creating a Barista
object via new
but you are not 'delete`'ing it afterwards.
Barista *b;
b = new Barista(); // <-- here
b = new JuniorBarista();
b->intro();
You need to get rid of that statement:
Barista *b;
b = new JuniorBarista();
b->intro();
And don't forget to delete b
when you are done using it:
Barista *b;
b = new JuniorBarista();
b->intro();
...
delete b;
Note, when delete
'ing an object via a pointer to a base class, the base class needs to have a virtual
destructor so that all derived destructors are called properly, but your code is currently missing that:
class Barista
{
public:
...
virtual ~Barista() = default; // <-- add this!
...
};
Upvotes: 2