具有函数声明/类型和定义的C++模板
我不熟悉模板,而且 此外,我知道有两种类型的模板,类模板和函数模板。然而,在我的代码片段中,我只想使用函数模板而不是类模板,但我想有一个使用该模板的函数声明和定义。在函数定义和声明中使用相同的模板代码似乎有点奇怪。我在cpp网站上读到了一个关于这个的帖子,但我现在只能发布两个链接 这是使用带有函数声明和定义的模板的正确语法吗 A. 以下是合并代码的片段:具有函数声明/类型和定义的C++模板,c++,templates,C++,Templates,我不熟悉模板,而且 此外,我知道有两种类型的模板,类模板和函数模板。然而,在我的代码片段中,我只想使用函数模板而不是类模板,但我想有一个使用该模板的函数声明和定义。在函数定义和声明中使用相同的模板代码似乎有点奇怪。我在cpp网站上读到了一个关于这个的帖子,但我现在只能发布两个链接 这是使用带有函数声明和定义的模板的正确语法吗 A. 以下是合并代码的片段: class GetReadFile { public: // Function Declaration template &l
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");
失败于
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");
请参阅。没错,或者您可以直接在类中内联定义函数。
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");