Frustrating array initialization problem

B

Bit byte

I have the ff code:

class MyClass {
public:
....

private:
typedef unsigned int (*hash_func_t) (void *data);
typedef int (*comp_func_t) (void *d1, void *d2);
typedef void (*destroy_func_t) (void *data);

typedef struct {
hash_func_t hash;
comp_func_t cmp;
destroy_func_t destroy;
}TypeFuncs;

TypeFuncs const functionTable[] = { //<- Compiler barfs here
//NOP
{},
//ID_STRING
{ (hash_func_t) &str_hash, (comp_func_t) &str_cmp, (destroy_func_t),
&str_destroy },
//STRING_ID
{ (hash_func_t) &int_hash, (comp_func_t) &int_cmp, (destroy_func_t),
&int_destroy },
//CONTRACT_DETAILS
{ (hash_func_t) &contract_hash, (comp_func_t) &contract_cmp,
(destroy_func) &contract_destroy },
...
};
};

Error msg:

syntax error : '{'
error C2334: unexpected token(s) preceding '{'; skipping apparent
function body'

This is a straight forward array initialization - why am I getting
compilation errors?
 
M

mlimber

Bit said:
I have the ff code:

class MyClass {
public:
....

private:
typedef unsigned int (*hash_func_t) (void *data);
typedef int (*comp_func_t) (void *d1, void *d2);
typedef void (*destroy_func_t) (void *data);

typedef struct {
hash_func_t hash;
comp_func_t cmp;
destroy_func_t destroy;
}TypeFuncs;

TypeFuncs const functionTable[] = { //<- Compiler barfs here
//NOP
{},
//ID_STRING
{ (hash_func_t) &str_hash, (comp_func_t) &str_cmp, (destroy_func_t),
&str_destroy },
//STRING_ID
{ (hash_func_t) &int_hash, (comp_func_t) &int_cmp, (destroy_func_t),
&int_destroy },
//CONTRACT_DETAILS
{ (hash_func_t) &contract_hash, (comp_func_t) &contract_cmp,
(destroy_func) &contract_destroy },
...
};
};

Error msg:

syntax error : '{'
error C2334: unexpected token(s) preceding '{'; skipping apparent
function body'

This is a straight forward array initialization - why am I getting
compilation errors?

You can only initialize integral constants inline in a class
declaration. You'll need to make that a static const and initialize it
at the point where you define it (i.e., in the source file where you
define the class' member functions and such), or you'll need to use
another technique if making it static is not an option (cf.
http://groups.google.com/group/comp.lang.c++/msg/e2fe5982913d4414).

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top