0

I am getting the following error:

error LNK2001: unresolved external symbol "public: virtual class AElementActor * __cdecl ElementFactory::CreateElement(int,int,int)" (?CreateElement@ElementFactory@@UEAAPEAVAElementActor@@HHH@Z)

with the following layout:

IElementFactory.h

struct IElementFactory
{

public:

    virtual AElementActor* CreateElement(
        int NeutronCount,
        int ProtonCount,
        int ElectronCount) = 0;

};

ElementFactory.h

#include "IElementFactory.h"

class ElementFactory : public IElementFactory
{

public:
    
    AElementActor* CreateElement(
        int NeutronCount,
        int ProtonCount,
        int ElectronCount) override;

};

ElementFactory.cpp

AElementActor* ElementFactory::CreateElement(
    int NeutronCount,
    int ProtonCount,
    int ElectronCount)
{
    // ...
}

If I move the definition to the header file for CreateElement, the program then compiles correctly:

ElementFactory.h updated

#include "IElementFactory.h"

class ElementFactory : public IElementFactory
{

public:
    
    AElementActor* CreateElement(
        int NeutronCount,
        int ProtonCount,
        int ElectronCount) override
    {
        // ...
    }

};

What gives?

Must pure virtuals be defined by the header or atleast, all in the cpp file?

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233

0 Answers0