c++ - C++ 交叉包含——这正常吗?
问题描述
我学习 C++ 已经有一段时间了(没那么久),现在我遇到了一个问题:
#ifndef _FILE_A_H
#define _FILE_A_H
template <typename T>
class A {
void func();
/// ... some code here
};
#include "a.cpp"
#endif
我想将 A 类的实现放在文件中'a.cpp'
。但要做到这一点,我需要包括'a.h'
. 在这种情况下交叉包含文件是否正常?
我有这样的东西'a.cpp'
(它正在编译但看起来很尴尬):
#ifndef _FILE_A_CPP
#define _FILE_A_CPP
#include "a.h"
template <typename T>
void A<T>::func() {
/// ... some code here
}
/// ... and some code here
#endif
解决方案
感谢@Magix 的回答。现在我a.cpp
改为a.tpp
并看起来像这样:
#include <iostream>
template <typename T>
void A<T>::func() {
/// ... some code here
}
/// ... and some code here
推荐阅读
- python - 用 3 个列表绘制曲面图
- javascript - 如果它们足够接近,我正在尝试比较两组坐标和绘图匹配,但代码不会编译?
- qt - Qthread exec()、事件循环和与上下文的连接
- rust - 函数调用中的生命周期参数有冲突的要求
- node.js - 使用邮递员向nodejs whit Express发送请求时出错?
- file - 有没有办法在windows下用erlang打开文件并锁定
- node.js - 如何在 facebook API 调用中选择用户或页面
- c# - 依赖调用和请求调用之间的时间差异很大
- if-statement - libreoffice calc 错误或错误配置阻止 IF 语句在版本 6.4.5.2 上工作
- android - 连续向不同的特征通知和写入特征