函数模板错误 -- 尚未声明
error with function template -- has not been declared
我正在做一些练习来理解C++模板。我的目的是做一个函数模板来改变模板类基础中的行为。
我收到以下错误消息:
In file included from main.cpp:2:0:
test1.h: In function ‘int my::fun(char*, int)’:
test1.h:12:26: error: ‘my::T’ has not been declared
简化的文件如下
------文件测试1.h -------
#ifndef TEST_1_H
#define TEST_1_H
#include "test2.h"
namespace my
{
template <typename T = myclass>
int fun(char* str,int dim)
{
return my::T::fun(str,dim);
}
}
#endif
-----文件测试2.h -------
#ifndef TEST_2_H
#define TEST_2_H
namespace my
{
struct myclass
{
static int fun(char* str,int dim);
};
}
#endif
------文件测试2.cpp --------
#include "test2.h"
namespace my
{
int myclass::fun(char* str,int dim)
{return 0;}
}
-----文件主.cpp -------
#include "test2.h"
#include "test1.h"
int main()
{}
你能帮我找出错误在哪里吗?
提前谢谢。
名称T
是模板参数的标识符。它不存在于任何命名空间中。参数名称或局部变量也不能限定。只需删除my::
即可。它似乎是一个剩余的 frim 一个使用my::myclass
代码版本,这不是一个函数模板。
有了限定条件,您将引用命名空间范围内的名称:
namespace my {
struct T {};
template <typename T>
void f() {
my::T from_namespace;
T from_template_argument;
}
}
相关文章:
- 在气泡排序程序中未声明错误功能
- 我在范围内未声明的错误类有问题
- 未在此范围内声明错误 'xy'
- 我的代码中有错误,未声明的标识符
- 错误:在 C++ 'std'使用未声明的标识符
- 未声明的标识符错误,但变量定义正确 (?)
- 在此范围内未声明错误"Clreol"
- 错误:在 C++ 目标 c 混合'self'使用未声明的标识符
- 错误: C2065: "QTcpSocket" : 未声明的标识符
- C++:outFile错误:outFile未声明
- "inet_ntoa"未声明错误
- C++成员函数未声明错误时(当它看起来是错误时)
- 代码块 HelloWorld C 示例不起作用:未声明错误"ptrdiff_t"
- 枚举包含在标头中 |未声明错误
- 读取未声明错误消息
- C++随机数猜测游戏-变量未声明错误
- 2D矢量在类中调整大小.在此范围内未声明错误
- 在此范围内未声明错误
- 无法调用 VBO 实现所需的函数...未声明错误
- 未声明错误 Nullptr