By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

Frustrating array initialization problem

P: n/a
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?

Apr 17 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Bit byte wrote:
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....e5982913d4414).

Cheers! --M

Apr 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.