![](/img/trans.png)
[英]templates - Unable to match function definition to an existing declaration c++
[英]C++ Templates with function declaration/ protype and definition
我是模板的新手, 正在阅读它们,并在模板上 找到了不错的视频教程 。
此外,我知道模板有两种类型,即类和函数模板。 但是,在我的代码片段中,我只想使用函数模板而不是类模板,但是我想拥有一个利用模板的函数声明和定义。 在函数定义和声明中为模板使用相同的代码似乎有点奇怪(我在cpp网站上读到了一个线程,但是现在只能发布两个链接)。
这是将模板与函数声明和定义一起使用的正确语法吗?
这是合并代码的片段:
class GetReadFile {
public:
// Function Declaration
template <size_t R, size_t C> // Template same as definition
bool writeHistory(double writeArray[R][C], string path);
};
// Function Definition
template <size_t R, size_t C> // Template same as declaration
bool GetReadFile::writeHistory(double writeArray[R][C], string path){...}
如果您以正确的方式调用它,那么语法对我来说效果很好:
GetReadFile grf;
double array[5][8];
grf.writeHistory<5,8>(array,"blah");
观看现场演示 。
不过请注意:
简单地调用该方法而不指定实际的数组尺寸,编译器将无法自动推导这些尺寸:
grf.writeHistory(array,"blah");
与失败
main.cpp:24:34: error: no matching function for call to 'GetReadFile::writeHistory(double [5][8], const char [5])'
grf.writeHistory(array,"blah");
^
...
main.cpp:10:10: note: template argument deduction/substitution failed:
main.cpp:24:34: note: couldn't deduce template parameter 'R'
grf.writeHistory(array,"blah");
请参阅替代演示 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.