[英]Template function is not a member of a namespace
I have a header file, RandFunctions.hpp
which contains a template function, 我有一个头文件
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
and another header file, Search.hpp
which includes RandFunctions.hpp
, 还有另一个头文件
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
when I include Search.hpp
in a cpp
file, for example, 例如,当我在
cpp
文件中包含Search.hpp
时,
#include "Search.hpp"
int
main(int argc, char** argv)
{
CTest<float> test;
return(0);
}
I get the following compile time error: 我收到以下编译时错误:
‘RealRandom’ is not a member of ‘surena::common’
What is wrong here? 怎么了
Since RealRandom
is a template function with no parameters, you need to provide a template argument: 由于
RealRandom
是不带参数的模板函数,因此需要提供一个模板参数:
GenRand(){ return common::RealRandom<RealT>(); }
^^^^^^^
Also in your main you'd have to qualify your test
variable with the proper namespaces: 同样在您的主目录中,您还必须使用适当的名称空间对
test
变量进行限定:
surena::search::CTest<float> test;
^^^^^^^^^^^^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.