I am a beginner so this problem might seem trivial to you. So I have the following files:
- base.h
- derived.h
- base.cpp
- derived.cpp
- TestCpp.cpp
base.h
#include <iostream>
namespace App
{
class Base
{
public:
virtual void Print();
};
}
base.cpp
#include "base.h"
namespace App
{
}
derived.h
#include "base.h"
class Derived : public App::Base
{
public:
void Print();
};
derived.cpp
#include "derived.h"
void Derived::Print()
{
std::cout << "This works!! form Derived class\n";
}
and at last TestCpp.cpp
#include "derived.h"
int main()
{
std::cout << "Hello World!\n";
Derived d;
d.Print();
return 0;
}
I am getting the following Linker error:

I don't know what it is I am doing wrong. Please help me out.
Just add a definition for
Print()in thebase.cppfile:It may not have anything inside it, but it should be there.
Or indicate
Print()to be defined in the derived class by making it pure:Any one of the 2 options work. Also in your derived class it's better to make
Print()asoverride: (Click here to know why)