【发布时间】:2021-01-24 21:53:31
【问题描述】:
我有以下功能:
template <typename DATA_T, std::size_t K, std::size_t F>
void write_raw_file(const std::string& filename, Eigen::Matrix<DATA_T, K, F>& data) {
std::fstream out_file = std::fstream(filename, std::ios::out | std::ios::binary);
for(std::size_t i = 0; i < data.rows(); i++) {
for(std::size_t j = 0; j < data.cols(); j++) {
out_file.write((char*) &data(i,j), sizeof(DATA_T));
}
}
out_file.close();
}
我是这样称呼它的:
template <typename DATA_T, typename K>
class my_class {
void my_func() {
std::string filename = "my_file.raw";
Eigen::Matrix<DATA_T, K, static_cast<std::size_t>(1)> my_mat;
write_raw_file(filename, my_mat);
}
};
int main() {
my_class<float, 16> obj();
obj.my_func();
}
当我收到此错误时,编译器似乎无法匹配模板类型:
error: no matching function for call to ‘write_raw_file(std::string&, Eigen::Matrix<float, 128, 1, 0, 128, 1>&)’
note: candidate: ‘template<class DATA_T, long unsigned int K, long unsigned int F> void write_raw_file(const string&, Eigen::Matrix<DATA_T, K, F>&)’
void write_raw_file(const std::string& filename, Eigen::Matrix<DATA_T, K, F>& data) {
note: template argument deduction/substitution failed:
note: mismatched types ‘long unsigned int’ and ‘int’
write_raw_file(filename, my_mat);
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
我对 C++/模板还很陌生。所以这有点令人困惑。我已经搜索过类似的情况,但这是一个非常基本的用例,我不知道它为什么会失败。
我用 std::string& 和 Eigen::Matrix
【问题讨论】:
-
K是如何定义的?
-
已澄清。我调用的函数在一个模板类中,以 DATA_T 和 K 作为模板参数。
-
你是如何实例化 my_class 的?