2011-03-31 65 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;}