Thomas
Thomas

Reputation: 2959

gcc fails to compile static function in namespace

I have the following declaration.

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){}

The problem is when compiling I get the following error.

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

Thanks in advance

Upvotes: 3

Views: 3551

Answers (2)

Arvid
Arvid

Reputation: 11245

The problem is that you have different signatures of the declaration and definition (the second argument being a const pointer versus non-const pointer). The compiler expected you to declare the non-const version inside the test namespace, but it can't find it (it only finds the declaration with a const pointer).

static functions in namespaces works fine. This builds in GCC 4.0.1:

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

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

int main() { return 0;}

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

The function you declared is different from the one you tried to define: the second parameter is "const" in the declaration, and not in the definition. Those are two different functions.

Upvotes: 6

Related Questions