0

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: enter image description here

I don't know what it is I am doing wrong. Please help me out.

  • You have clearly realized that you need to provide an actual *definition* for `Derived::Print()`, so what makes you think things are different for `Base::Print()`? – Adrian Mole Feb 28 '22 at 11:39
  • You need to add a definition of `virtual void Print();` in `Base` or make it pure virtual by writing `virtual void Print() = 0;` instead. – user17732522 Feb 28 '22 at 11:39
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix), specifically [this answer](https://stackoverflow.com/a/12574407/17732522). – user17732522 Feb 28 '22 at 11:41
  • @user17732522 that did work out. I thought if you use the keyword "virtual" you had to skip the definition in the base class. – Anirudh Kanaparthy Feb 28 '22 at 11:52
  • @AnirudhKanaparthy A _pure virtual_ function doesn't need a definition, but simply a _virtual_ function does, because you can still create a `Base` object directly and call `.Print()` on it. Which function definition should then be called? If you make it pure virtual, the class becomes an _abstract class_, for which only derived instances can be created, so the definition isn't required anymore. – user17732522 Feb 28 '22 at 11:55

2 Answers2

1

The problem is that you've only declared the virtual function Print in class Base but not defined it.

And from C++03 Standard: 10.3 Virtual functions [class.virtual]

A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).

So the way to solve this problem would be to either implement/define the virtual member function Print in class Base or make it pure virtual by replacing the declaration with virtual void Print() = 0; inside class Base

Solution 1

Base.cpp

#include "base.h"

namespace App
{
    void Base::Print()
    {
        
    }
}

Solution 1 DEMO

Solution 2

Base.h

#include <iostream>

namespace App
{
    class Base
    {
    public:
        virtual void Print() = 0;
    };
}

Solution 2 DEMO

Jason
  • 36,170
  • 5
  • 26
  • 60
0

Just add a definition for Print() in the base.cpp file:

namespace App
{
    void Base::Print() { }
}

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:

virtual void Print() = 0;

Any one of the 2 options work. Also in your derived class it's better to make Print() as override: (Click here to know why)

class Derived : public App::Base
{
public:
    void Print() override;
};
The Coding Fox
  • 1,488
  • 1
  • 4
  • 18