假设我在单独的 foo
文件中定义了两个类 bar
和 .hpp
。它们都包含一个头文件,其中包含名为 baz
的类的定义。我想在名为 foo
的文件中创建类类型为 bar
和 test.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
文件同时使用 foo
和 bar
对象?