2011-03-31 66 views
3

我有以下声明。gcc无法编译名称空间中的静态函数

namespace test{ 
static cl_option* find_opt(const int &val, const cl_option *options); 
} 

test::cl_option* test::find_opt(const int &val, cl_option *options){} 

问题是编译时出现以下错误。

error: ‘test::cl_option* test::find_opt(const int&, test::cl_option*)’ should have been declared inside ‘test’ 

在此先感谢

回答

6

你声明的功能是你试图定义一个不同:第二个参数是“常量”的声明,而不是定义。这是两种不同的功能。

3

问题是你有不同的声明和定义的签名(第二个参数是一个const指针与非const指针)。编译器希望你在test命名空间内声明非const的版本,但它找不到它(它只能找到带有const指针的声明)。

名称空间中的静态函数可以正常工作。这建立在GCC 4.0.1中:

namespace test { 
    struct B {}; 
    static B* a(); 
} 

test::B* test::a() {} 

int main() { return 0;}