error: undeclared here (not in a function)

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all, I ask you a tip. I've this code.

typedef struct menuitem{
	char item[LCD_LINE_LENGTH + 1];
	struct menuitem *next;
	void (*func)();
} menuItem;
const menuItem mIcoll[4] EEMEM = {
	{"abc             \0", NULL, NULL},
	{"def             \0", NULL, NULL},
	{"ghi             \0", NULL, NULL},
	{"<-- prev.       \0", mImain, NULL}	};
const menuItem mIimp[4] EEMEM =  {
	{"abc             \0", NULL, NULL},
	{"def             \0", NULL, NULL},
	{"ghi             \0", NULL, NULL},
	{"<-- prev.       \0", mImain, NULL}	};
const menuItem mImain[6] EEMEM = {
	{"abc             \0", NULL, NULL}, 
	{"def             \0", mIcoll, NULL},
	{"ghi             \0", mIimp, NULL}	};

Obviously the compiler returns this error:

error: 'mImain' undeclared here (not in a function)

How can I fix this?
for now I am doing something like this:

const menuItem mIcoll[4] EEMEM = {
....
	{"<-- prev.       \0", mIcoll, NULL}	};
const menuItem mIimp[4] EEMEM =  {
....
	{"<-- prev.       \0", mIimp, NULL}	};
const menuItem mImain[6] EEMEM = {
....
	{"def             \0", mIcoll, NULL},
	{"ghi             \0", mIimp, NULL}	};

and then in the code I will do the necessary controls..

Any better idea?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The compiler needs to know, that mImain is a function.
So put a

void mImain ( void );

in front of your current code.
(or in a appropriate header file)

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This appears to work (well it *builds* anyway! ;) ):

#include  
#include 
#define LCD_LINE_LENGTH	16

typedef struct menuitem{ 
   char item[LCD_LINE_LENGTH + 1]; 
   struct menuitem *next; 
   void (*func)(); 
} menuItem; 

extern const menuItem mImain[6] EEMEM;

const menuItem mIcoll[4] EEMEM = { 
   {"abc             \0", NULL, NULL}, 
   {"def             \0", NULL, NULL}, 
   {"ghi             \0", NULL, NULL}, 
   {"<-- prev.       \0", mImain, NULL}   }; 

const menuItem mIimp[4] EEMEM =  { 
   {"abc             \0", NULL, NULL}, 
   {"def             \0", NULL, NULL}, 
   {"ghi             \0", NULL, NULL}, 
   {"<-- prev.       \0", mImain, NULL}   }; 

const menuItem mImain[6] EEMEM = { 
   {"abc             \0", NULL, NULL}, 
   {"def             \0", mIcoll, NULL}, 
   {"ghi             \0", mIimp, NULL}   };

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The compiler needs to know, that mImain is a function.

Except it isn't - it's a forward reference to the name of the third array.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
This appears to work (well it *builds* anyway! ;) ):

extern const menuItem mImain[6] EEMEM;

Yeah, this is what I want..
I did not know the extern directive.
Thanks so much.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well this is an "odd" use of extern as it usually means "this variable is not defined within this file so just put in a placeholder for the time being and the linker will later fix up the reference to the file where it actually resides". Except that in this case it IS in the same file - but you are still allowed to use "extern" in this way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Except it isn't - it's a forward reference to the name of the third array
Oops! :oops:

I thought it is the initialization of "void (*func)();".
Actually the third element, not the second. ;-)

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, now I have a few seconds more time to view the code. ;-)

The "correct" way is a simple forward declaration of the array:

const menuItem mImain[];

BTW: You should change the struct member a little:

struct menuitem *next;

->

const struct menuitem *next;

Stefan Ernst