两个文件在 c++ 中相互包含问题
我正在为一个开源库做出一些贡献,但我在修改其他人的代码时遇到了麻烦。以前,该库有一个名为 IntervalT.h
的文件和一个名为 Curves.h
的文件,其实现位于文件 Curves.tcc
中,并且 <由于某种原因,code>Interval.h 包含 Curves.h
。现在我需要在 Curves.h
中使用 IntervalT.h
,但是当我尝试使用 IntervalT
中定义的类时code>IntervalT.h,编译器给我错误(我已经在 Curves.h
文件的开头包含了 IntervalT.h
):
../../../inc/CORE/poly/Curves.h:1337:3: error: ‘IntervalT’ does not name a type
我的问题是:因为我从来没有以前有过这样的经历,“没有命名类型
”错误是否与c++头文件的相互包含有关?或者是其他错误导致了这个错误?如果是这样,我应该如何编写程序让 Curves.h 看到 IntervalT.h?
顺便说一句,这段代码的组织方式非常奇怪。 Curves.tcc
实际上包含在Curves.h
中,这与我们通常的做法相反。这样做有什么特别的理由吗?或者说这其实并不重要? .tcc 扩展名到底是什么?
I'm doing some contribution to an open source library, but I'm having trouble modifying other people's code. Previously the library had a file called IntervalT.h
and a file called Curves.h
with the implementation in the file Curves.tcc
, and Interval.h
includes Curves.h
for some reason. Right now I need to use IntervalT.h
in Curves.h
, but when I tried to use the class IntervalT<NT>
defined in IntervalT.h
, the compiler gives me error (I've already included IntervalT.h
in the beginning of Curves.h
file):
../../../inc/CORE/poly/Curves.h:1337:3: error: ‘IntervalT’ does not name a type
My question is: Since I never have had such experience before, is "does not name a type
" error related to mutual inclusion of c++ header files? Or it is other mistakes that cause this error? If so, how should I write my program to let the Curves.h sees IntervalT.h?
By the way, this piece of code was organized in a very weird way. Curves.tcc
is actually included by Curves.h
, which is the reverse way of we usually do. Is there a particular reason to do this? Or it doesn't really matter? And what is .tcc
extension after all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只能猜测(因为你可以随意命名你的文件),但是
.tcc
扩展名是.cc
扩展名的修改(这只是 C++ 代码)但t
代表template
。Curves.h
包含Curves.tcc
这一事实证实了这一点,这是一种将接口与模板实现分离的常用技术,同时仍然不会扰乱编译器,期望模板化代码的声明和定义都在同一个文件中(否则会出现错误)。 查看此答案以获得更好的想法。没有命名类型
可能是因为IntervalT
是一个模板,而您没有提供模板参数(或者可能是一个不同的错误,我没有尝试过)它),或者即使该文件名为IntervalT.h
,但它不包含名为IntervalT
的类,或者它位于不同的命名空间中。在我们做出更好的诊断之前,您必须提供更多信息。I can only guess (because you can name your files whatever you want), but the
.tcc
extension is a modification of the.cc
extension (which is just C++ code) but thet
stands fortemplate
. This is confirmed by the fact thatCurves.h
includesCurves.tcc
which is a common technique for separating the interface from the implementation of a template while still not upsetting the compiler which expects both declaration and definition of templated code in the same file (or you get an error). See this answer to get a better idea.The
does not name a type
could be thatIntervalT
is a template and you're not providing a template parameter (or that could be a different error, I haven't tried it), or that even though the file is calledIntervalT.h
it does not contain a class calledIntervalT
, or that it's in a different namespace. You'll have to give more info before we can make a better diagnosis.