C++:非内联时构造函数/析构函数未解析?

发布于 2024-08-31 00:56:51 字数 796 浏览 2 评论 0原文

在基于插件的 C++ 项目中,我有一个 TmpClass 用于在主应用程序和插件之间交换数据。因此,相应的 TmpClass.h 包含在主应用程序项目包含的抽象插件接口类中,并由每个插件实现。

由于插件在 TmpClass 实例的 STL 向量上工作,因此需要有一个 TmpClass 的默认构造函数和析构函数。我已在 TmpClass.h 中声明了这些:

class TmpClass {
  TmpClass();
  ~TmpClass();
}

并在 TmpClass.cpp 中实现了它们。

TmpClass::~TmpClass() {}
TmpClass::TmpClass() {}

但是,在编译插件时,这会导致链接器抱怨两个未解析的外部对象 - std::vector 模板实例化所需的 TmpClass 默认构造函数和析构函数- 即使我在 TmpClass.h 中声明并在 TmpClass.cpp 中实现的所有其他函数都可以工作。一旦我从 .cpp 文件中删除(空)默认构造函数和析构函数并将它们内联到 .h 文件中的类声明中,插件就会编译并工作。

为什么默认构造函数和析构函数必须内联才能编译此代码?为什么它很重要? (我正在使用 MSVC++8)。

In a plugin-based C++ project, I have a TmpClass that is used to exchange data between the main application and the plugins. Therefore the respective TmpClass.h is included in the abstract plugin interface class that is included by the main application project, and implemented by each plugin.

As the plugins work on STL vectors of TmpClass instances, there needs to be a default constructor and destructor for the TmpClass. I had declared these in TmpClass.h:

class TmpClass {
  TmpClass();
  ~TmpClass();
}

and implemented them in TmpClass.cpp.

TmpClass::~TmpClass() {}
TmpClass::TmpClass() {}

However, when compiling plugins this leads to the linker complaining about two unresolved externals - the default constructor and destructor of TmpClass as required by the std::vector<TmpClass> template instantiation - even though all other functions I declare in TmpClass.h and implement in TmpClass.cpp work. As soon as I remove the (empty) default constructor and destructor from the .cpp file and inline them into the class declaration in the .h file, the plugins compile and work.

Why is it that the default constructor and destructor have to be inline for this code to compile? Why does it even maatter? (I'm using MSVC++8).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-09-07 00:56:51

您描述的行为只是意味着您忘记将 TmpClass.cpp 文件包含到项目中。

编译器不能也不会神奇地知道非内联类方法的定义位置。您有责任编译所有 .cpp 文件并将它们链接在一起。在 MSVC 中,通常通过将所有 .cpp 文件添加到项目中来完成。

The behavior you describe simply means that you forgot to include the TmpClass.cpp file into the project.

The compiler cannot and will not magically know where the non-inline class methods are defined. It is your responsibility to compile all .cpp files and link them together. In MSVC it is normally done by adding all .cpp files to the project.

离笑几人歌 2024-09-07 00:56:51

我猜您有一个“主应用程序”项目和一个或多个“插件”项目,而且您似乎尚未将 TmpClass.cpp 包含在插件项目中。我还猜测“您在 .h 中声明并在 .cpp 中实现的所有其他函数”仅由您的主项目使用,而不是由您的插件使用。

正如其他人所说,您可以在插件项目中包含 TmpClass.cpp。另一个选项是创建一个 dll“sdk”项目并将主项目和插件项目链接到它。

如果您仍然认为这个和 AndreyT 的答案是错误的,您应该提供一些有关您的项目结构的更多信息。

I'm guessing you have one "main application" project and one or more "plugin" projects and it looks like you haven't included TmpClass.cpp in the plugins project. I'm also guessing "all other function you declare in the .h and implement in .cpp" are only used by your main project and not by your plugins.

As others have said, you can include TmpClass.cpp across your plugin projects. The other option is to create a dll "sdk" project and link both the main and plugins project against it.

If you still think this and AndreyT's answers are wrong, you should provide some more information about your projects structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文