重定义错误:多个类包含相同的 .hpp 文件

时间:2021-07-18 18:32:16

标签: c++

假设我在单独的 foo 文件中定义了两个类 bar.hpp。它们都包含一个头文件,其中包含名为 baz 的类的定义。我想在名为 foo 的文件中创建类类型为 bartest.cpp 的对象当我尝试使用以下 Makefile 进行编译时,出现错误:

In file included from test.cpp:10:
In file included from ./bar.hpp:4:
./baz.hpp:11:7: error: redefinition of 'Baz'
class Baz {
      ^
./baz.hpp:11:7: note: previous definition is here
class Baz {
      ^
In file included from test.cpp:10:

我正在尝试学习 C++ 最佳实践,显然有两个文件都包含同一个文件是一个很大的禁忌。组织代码的最佳方式是什么,以便我可以让 .cpp 文件同时使用 foobar 对象?

0 个答案:

没有答案