如何将模板定义和实例化分为HPP和IPP文件
How do I split template defintion and instantiation into hpp and ipp files
我想将模板类分为两个文件,例如正常类如何在声明所在的.hpp和.hpp and a的.ipp中实现。
我已经使用了普通方法。但是,使用本身就是模板的方法,我正面临一些问题。
使用以下结构:
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include <array>
#include <type_traits>
template<typename t,
std::size_t m,
std::size_t n>
class Matrix
{
static_assert(std::is_arithmetic<t>::value,
"Matrix can only be declared with a type where std::is_arithmetic is true.");
public:
Matrix();
template<std::size_t y, std::size_t x, std::size_t p, std::size_t q>
Matrix<t, p, q> slice() const;
private:
std::array<std::array<t, n>, m> data{};
};
#include "Matrix.ipp"
#endif
matrix.ipp:
#include "Matrix.hpp"
template<typename t, std::size_t m, std::size_t n>
Matrix<t, m, n>::Matrix()
{}
template<typename t,
std::size_t m,
std::size_t n,
std::size_t y,
std::size_t x,
std::size_t p,
std::size_t q>
Matrix<t, p, q> Matrix<t, m, n>::slice() const
{
auto mat = Matrix<t, p, q>();
for (std::size_t i = y; i < m; i++)
{
for (std::size_t j = x; j < n; j++)
{
mat[i - y][j - x] = (*this)[i][j];
}
}
return mat;
}
main.cpp
#include "Matrix.hpp"
int main() {
auto m = Matrix<3, 3, int>();
auto sliced = m.template slice<1, 1, 2, 2>();
return 0;
}
现在,当我对此进行编译时,它会在以下消息中失败:
../Matrix.ipp:14:17: error: prototype for ‘Matrix<t, p, q> Matrix<t, m, n>::slice() const’ does not match any in class ‘Matrix<t, m, n>’
Matrix<t, p, q> Matrix<t, m, n>::slice() const
^~~~~~~~~~~~~~~
In file included from ../main.cpp:0:0:
../Matrix.hpp:20:18: error: candidate is: template<class t, long unsigned int m, long unsigned int n> template<long unsigned int y, long unsigned int x, long unsigned int p, long unsigned int q> Matrix<t, p, q> Matrix<t, m, n>::slice() const
Matrix<t, p, q> slice() const;
我不确定如何处理此问题,因为编译器无法识别它。有办法解决此问题吗?
我希望有人可以帮助我。谢谢!
您需要定义两组模板参数:一个用于矩阵类模板,另一个用于矩阵类功能,而不是将它们合并到一个长列表中:
template<typename t, std::size_t m, std::size_t n>
template<std::size_t y, std::size_t x, std::size_t p, std::size_t q>
Matrix<t, p, q> Matrix<t, m, n>::slice() const
{
还要注意,您的包含实际上是混乱的。包括" matrix.hpp"进入.ipp文件不起作用,因为您已经在" matrix.hpp"中包含了" matrix.ipp"。尽管这不会导致错误。
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 什么是"#include <boost/functional/hash.hpp> "?
- 使用Vulkan hpp vk::enumerateInstanceVersion()会导致segfault
- 如何将class.cpp和class.hpp编译为一个.o文件
- g++ -I 和 -L 命令来查找不起作用的 .hpp 文件
- VS Code C++:不准确的系统包括路径错误(wchar.h,boost/lambda/lambda.hpp)
- Gnuplot_i.hpp C++接口绘制局部变量而不是文件
- 致命错误:找不到'boost/uuid/uuid.hpp'文件
- BGL:当包含 random_spanning_tree.hpp 时,对strong_components的调用无法编译
- 造成致命错误:boost/fusion/iterator/equal_to.hpp 没有这样的文件或目录
- 无法使包含.hpp文件与其中的2个文件一起使用
- 在指向现有内存地址的 hpp 文件中声明成员函数的最佳方法
- 无法打开包含文件'Graphics.hpp'没有这样的文件或目录,Visual Studio的其他包含不起作用
- Ubuntu 下的 Cmake 和 Intel IPP 的编译问题
- 编译器正在查找.当"实施"完全在"我的"中时的CPP文件.HPP文件
- 我的项目中某些仅标头文件不能包含使用 CMake 和 Vscode 的"SFML/Graphics.hpp"
- QtCreator使用CMAKE_C_FLAGS来解释C++(hpp)标头
- 如何正确使用hpp文件和cpp文件中的嵌套类
- SIFT detectAndCompute引发ipp异常
- 如何将模板定义和实例化分为HPP和IPP文件