C++ Array Of Function Pointers problem
-
CNewbie wrote: What I want to do is define the 2 function pointer arrays with the appropriate functions globally so that I can use them throughout the problem without having to redefine them each time I have to use them. Right. The compiler is never happy when it encounters a redefinition. You can certainly make the arrays global to the entire program. However, they must be initialized within some function (e.g.,
main()
). After that, they can be used from wherever they are needed. And, as you have already discovered, if the arrays are needed in more than one file, you'll need to employ theextern
keyword. This means the arrays are visible from files other than the one in which they are defined. CNewbie wrote: My Class is CDialog. Does this mean that you are not using MFC? I'm sure you've figured this much out:void (*call_cmd_function[160])(void);
void Func1( void )
{
cout << "Func1" << endl;
}void Func2( void )
{
cout << "Func2" << endl;
}void Func3( void )
{
cout << "Func3" << endl;
}void main( void )
{
call_cmd_function[0] = Func1;
call_cmd_function[1] = Func2;
call_cmd_function[2] = Func3;(\*call\_cmd\_function\[2\])(); (\*call\_cmd\_function\[1\])(); (\*call\_cmd\_function\[0\])();
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
Actually I am using MFC. "CDialog" is a ficticious name. As I pointed out in the last post, I declared both of the arrays in my header file as such:
void (*call_cmd_function[160])(); void (*call_ins_function[48])();
Then I defined them "globally" at the top of one of my source files as such:void (CflowlogDlg::*call_ins_function[48])() = {NULL}; void (CflowlogDlg::*call_cmd_function[160])() = {NULL};
As you said without the "extern" keyword this would not be seen within all of my source files. The wierd thing is that I reference these arrays in more then one of my source files and the compiler didnt give me any errors when compiling with this code. Now I do define these arrays in my InitDialog() as such:CflowlogDlg INSFunction; INSFunction.call_ins_function[0] = &CflowlogDlg::doins00; INSFunction.call_ins_function[46] = &CflowlogDlg::doins5C;
but when I get to reference the array in another member function it is not defined. Instead of the array having the function addresses in them they have the default memory address of 0xCCCCCCCC in them. The definition only works if i put the definition of the arrays in the same member function as the one I reference them from. Now obviously I am doing something wrong here, but i do not know what -
Actually I am using MFC. "CDialog" is a ficticious name. As I pointed out in the last post, I declared both of the arrays in my header file as such:
void (*call_cmd_function[160])(); void (*call_ins_function[48])();
Then I defined them "globally" at the top of one of my source files as such:void (CflowlogDlg::*call_ins_function[48])() = {NULL}; void (CflowlogDlg::*call_cmd_function[160])() = {NULL};
As you said without the "extern" keyword this would not be seen within all of my source files. The wierd thing is that I reference these arrays in more then one of my source files and the compiler didnt give me any errors when compiling with this code. Now I do define these arrays in my InitDialog() as such:CflowlogDlg INSFunction; INSFunction.call_ins_function[0] = &CflowlogDlg::doins00; INSFunction.call_ins_function[46] = &CflowlogDlg::doins5C;
but when I get to reference the array in another member function it is not defined. Instead of the array having the function addresses in them they have the default memory address of 0xCCCCCCCC in them. The definition only works if i put the definition of the arrays in the same member function as the one I reference them from. Now obviously I am doing something wrong here, but i do not know whatCNewbie wrote: void (*call_cmd_function[160])(); void (*call_ins_function[48])(); Then I defined them "globally" at the top of one of my source files as such: void (CflowlogDlg::*call_ins_function[48])() = {NULL}; void (CflowlogDlg::*call_cmd_function[160])() = {NULL}; Here you have two separate copies of
call_ins_function
andcall_cmd_function
. Arecall_cmd_function
andcall_ins_function
supposed to be a member ofCflowlogDlg
?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
CNewbie wrote: void (*call_cmd_function[160])(); void (*call_ins_function[48])(); Then I defined them "globally" at the top of one of my source files as such: void (CflowlogDlg::*call_ins_function[48])() = {NULL}; void (CflowlogDlg::*call_cmd_function[160])() = {NULL}; Here you have two separate copies of
call_ins_function
andcall_cmd_function
. Arecall_cmd_function
andcall_ins_function
supposed to be a member ofCflowlogDlg
?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
See if this helps:
class CflowlogDlg
{
public:void Func1( void ) { cout << "Func1()" << endl; } void Func2( void ) { cout << "Func2()" << endl; } void Func3( void ) { cout << "Func3()" << endl; } typedef void (CflowlogDlg::\*call\_cmd\_function)(void);
};
void main( void )
{
CflowlogDlg::call_cmd_function arr[160];arr\[0\] = CflowlogDlg::Func1; arr\[1\] = CflowlogDlg::Func2; arr\[2\] = CflowlogDlg::Func3; CflowlogDlg m; (m.\*arr\[2\])(); (m.\*arr\[1\])(); (m.\*arr\[0\])();
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
See if this helps:
class CflowlogDlg
{
public:void Func1( void ) { cout << "Func1()" << endl; } void Func2( void ) { cout << "Func2()" << endl; } void Func3( void ) { cout << "Func3()" << endl; } typedef void (CflowlogDlg::\*call\_cmd\_function)(void);
};
void main( void )
{
CflowlogDlg::call_cmd_function arr[160];arr\[0\] = CflowlogDlg::Func1; arr\[1\] = CflowlogDlg::Func2; arr\[2\] = CflowlogDlg::Func3; CflowlogDlg m; (m.\*arr\[2\])(); (m.\*arr\[1\])(); (m.\*arr\[0\])();
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
That worked as local definitions, but didn't work for the global aspect. The reference of call_cmd_function and call_ins function as well as the class referencein my other source files go unresolved. I can pinpoint my problem to the fact that I need to globalize my class instance. I can lcoalize them by defining them in a member function like so: CflowlogDlg Instance; but again i dont know how to globalize it and extern it so it can be used throughout all of my source files. I tried doing it like I read in the c++ book I have, but it doesnt seen to work correctly as I get Linker errors.
-
That worked as local definitions, but didn't work for the global aspect. The reference of call_cmd_function and call_ins function as well as the class referencein my other source files go unresolved. I can pinpoint my problem to the fact that I need to globalize my class instance. I can lcoalize them by defining them in a member function like so: CflowlogDlg Instance; but again i dont know how to globalize it and extern it so it can be used throughout all of my source files. I tried doing it like I read in the c++ book I have, but it doesnt seen to work correctly as I get Linker errors.
At this point, I suggest you boil the problem down to just what is absolutely necessary. From there you can post a code snippet and we can put it to rest once and for all.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
At this point, I suggest you boil the problem down to just what is absolutely necessary. From there you can post a code snippet and we can put it to rest once and for all.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
Ok I decided to make both arrays static so that I wouldn't have to use any class instances to access them. Plus it boads better for my program to do it this way since all of the member functions that the array uses are already static.Now I know that static variable (even global ones) are only known within the file that it is declared in. Here is what I did: Within my Header file I declare the 2 arrays as static:
static void (*call_cmd_function[160])(); static void (*call_ins_function[48])();
Then I define them at the top of the source files where I reference them:void (CflowlogDlg::*call_cmd_function[160])() = {NULL}; //goes into flowlogcmds.cpp void (CflowlogDlg::*call_ins_function[48])() = {NULL}; //goes into flowlogins.cpp
When I compile I get unresolved external symbol errors:flowlogcmds.obj : error LNK2001: unresolved external symbol "public: static void (__cdecl** CflowlogDlg::call_cmd_function)(void)" (?call_cmd_function@CflowlogDlg@@2PAP6AXXZA) flowlogins.obj : error LNK2001: unresolved external symbol "public: static void (__cdecl** CflowlogDlg::call_ins_function)(void)" (?call_ins_function@CflowlogDlg@@2PAP6AXXZA)
neither are referenced outside of its file, so I dont know why I am getting the Linker errors. -
Ok I decided to make both arrays static so that I wouldn't have to use any class instances to access them. Plus it boads better for my program to do it this way since all of the member functions that the array uses are already static.Now I know that static variable (even global ones) are only known within the file that it is declared in. Here is what I did: Within my Header file I declare the 2 arrays as static:
static void (*call_cmd_function[160])(); static void (*call_ins_function[48])();
Then I define them at the top of the source files where I reference them:void (CflowlogDlg::*call_cmd_function[160])() = {NULL}; //goes into flowlogcmds.cpp void (CflowlogDlg::*call_ins_function[48])() = {NULL}; //goes into flowlogins.cpp
When I compile I get unresolved external symbol errors:flowlogcmds.obj : error LNK2001: unresolved external symbol "public: static void (__cdecl** CflowlogDlg::call_cmd_function)(void)" (?call_cmd_function@CflowlogDlg@@2PAP6AXXZA) flowlogins.obj : error LNK2001: unresolved external symbol "public: static void (__cdecl** CflowlogDlg::call_ins_function)(void)" (?call_ins_function@CflowlogDlg@@2PAP6AXXZA)
neither are referenced outside of its file, so I dont know why I am getting the Linker errors.if
call_cmd_function
,call_ins_function
,doins00
, anddoins5C
are allstatic
members, why put them in a class at all? That's seems to make things unnecessarily complicated. Does this work:class CflowlogDlg
{
public:static void Func1( void ) { cout << "Func1()" << endl; } static void Func2( void ) { cout << "Func2()" << endl; } static void Func3( void ) { cout << "Func3()" << endl; }
};
void (*call_cmd_function[3])(void);
void main( void )
{
call_cmd_function[0] = CflowlogDlg::Func1;
call_cmd_function[1] = CflowlogDlg::Func2;
call_cmd_function[2] = CflowlogDlg::Func3;(\*call\_cmd\_function\[2\])(); (\*call\_cmd\_function\[1\])(); (\*call\_cmd\_function\[0\])();
}
Now you can reference
call_cmd_function[]
from other files, too.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
if
call_cmd_function
,call_ins_function
,doins00
, anddoins5C
are allstatic
members, why put them in a class at all? That's seems to make things unnecessarily complicated. Does this work:class CflowlogDlg
{
public:static void Func1( void ) { cout << "Func1()" << endl; } static void Func2( void ) { cout << "Func2()" << endl; } static void Func3( void ) { cout << "Func3()" << endl; }
};
void (*call_cmd_function[3])(void);
void main( void )
{
call_cmd_function[0] = CflowlogDlg::Func1;
call_cmd_function[1] = CflowlogDlg::Func2;
call_cmd_function[2] = CflowlogDlg::Func3;(\*call\_cmd\_function\[2\])(); (\*call\_cmd\_function\[1\])(); (\*call\_cmd\_function\[0\])();
}
Now you can reference
call_cmd_function[]
from other files, too.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
CNewbie wrote: Finally got it working So what was the final solution?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown