Template函数不是命名空间的成员
Template function is not a member of a namespace
我有一个头文件RandFunctions.hpp
,它包含一个模板函数
#ifndef _RANDFUNCTIONS_HPP_
#define _RANDFUNCTIONS_HPP_
#include <stdlib.h>
#include <time.h>
namespace surena
{
namespace common
{
template<typename RealT> inline
RealT
RealRandom()
{
return rand()/(RealT(RAND_MAX)+1);
}
};
};
#endif
以及另一个头文件Search.hpp
,其包括RandFunctions.hpp
、
#ifndef _SEARCH_HPP_
#define _SEARCH_HPP_
#include "RandFunctions.hpp"
#include <stdlib.h>
#include <time.h>
namespace surena
{
namespace search
{
template<typename RealT>
class CTest
{
public:
CTest() {srand((unsigned)(time(0)));}
RealT
GenRand(){ return common::RealRandom(); }
};
};
};
#endif
当我在cpp
文件中包括Search.hpp
时,例如
#include "Search.hpp"
int
main(int argc, char** argv)
{
CTest<float> test;
return(0);
}
我得到以下编译时错误:
‘RealRandom’ is not a member of ‘surena::common’
这里怎么了?
由于RealRandom
是一个没有参数的模板函数,因此需要提供一个模板参数:
GenRand(){ return common::RealRandom<RealT>(); }
^^^^^^^
此外,在main中,您必须使用适当的名称空间限定test
变量:
surena::search::CTest<float> test;
^^^^^^^^^^^^^^^^
相关文章:
- 命名空间 std 没有成员any_of
- 为什么 Clang 在命名空间'std'中没有名为 'copy' 的成员给出错误
- 初始化命名空间中的变量是否类似于将它们初始化为类成员?
- 在 c++ 中使用类的命名空间定义类的成员
- Catalina C++:使用<cmath>标头产生错误:全局命名空间中没有名为 'signbit' 的成员
- 命名空间 Win 没有成员"simpleControl"是什么意思
- Visual Studio 2017:命名空间"System"没有成员"Windows"
- 如何从嵌套在命名空间中的类调用成员函数?
- 成员的结构与命名空间的潜在范围
- 类成员函数在其命名空间之外定义
- Typedef 不是命名空间的成员
- 命名空间 pcl 在点云库中没有名为 PointCloud 的成员
- 将typeID转换为静态成员访问(C )的命名空间
- 无法解析命名空间成员"线程"
- 命名空间:不能在没有对象的情况下调用成员函数
- "uintmax_t":不是"全局命名空间"的成员
- OpenCV CUDA 函数缺失 - 命名空间"cv:cuda"没有成员"resize"
- 错误:"成员"在此上下文中是私有的 - 命名空间
- 从另一个命名空间访问标准命名空间数据成员
- 使用命名空间进行函数成员定义