C++ 交叉包含——这正常吗?

标签 c++ class templates

我学习 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

关于C++ 交叉包含——这正常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50212003/

相关文章:

c++ - 不存在从 "const std::string"到 "time_t"的合适转换函数

ios - Objective-C 自定义类操作和导出

c++ - "undefined reference"到静态字段模板特化

c++ - 使用 typedef 的模板特化

c++ - C++中通过引用传递类成员函数返回值

c++ - 根据模板参数有条件地启用成员函数

从模板实例化后声明的模板函数选择候选者调用的 C++ 模板重载决策

c++ - 每个 cpp 的 nginx 语法高亮显示,无需人工交互

c++ - 隐式转换和多态性

java - UML三元关联实现java代码