在C++解决方案中重用基类时,MSVC链接器错误LNK2019

MSVC linker error LNK2019 when reusing a base class in C++ solution

本文关键字:MSVC 链接 LNK2019 错误 基类 解决方案 C++      更新时间:2023-10-16

这真的让我大吃一惊,Visual Studio 2008是如此的无脑,以至于它对以下错误毫无帮助。

我的C++解决方案中有几个项目。其中一个项目在crashreporter.h和crashreporter.cpp文件中有基本的C++类CCrashReporter。

我需要从它派生另一个类,该类位于同一解决方案中的另一个项目中。所以我做了:

//From CrashReporter2.h
#pragma once
#include "..BaseModulesCrashReporter.h"
class CCrashReporter2 :
    public CCrashReporter
{
public:
    CCrashReporter2(void);
    virtual ~CCrashReporter2(void);
};

然后:

//From crashreporter2.cpp
#include "StdAfx.h"
#include "CrashReporter2.h"

CCrashReporter2::CCrashReporter2(void):
CCrashReporter(ENTERY_PARAM_FOR_REPORTER2)
{
}
CCrashReporter2::~CCrashReporter2(void)
{
}

上面的代码编译得还可以,但当链接器运行时,我会得到以下代码:

1>CrashReporter2.obj : error LNK2019: unresolved external symbol "public: __thiscall CCrashReporter::CCrashReporter(int) blah-blah
1>CrashReporter2.obj : error LNK2019: unresolved external symbol "public: __thiscall CCrashReporter::~CCrashReporter(void) blah-blah
1>C:UsersDevC++ProjName123DebugMod123.exe : fatal error LNK1120: 2 unresolved externals

请参阅Bo Persson的评论。每个项目都需要以这样或那样的方式链接到正确的文件。将源文件直接添加到项目中,或者创建一个链接到应用程序的静态/动态库项目。