|
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.
The 2 function pointer arrays are as follows:
void (*call_cmd_function[160])();
void (*call_ins_function[48])();
The functions (NOTE: I did declare them in my class, just didnt put them in the class example above) for both of these arrays are named "dFunction 1 through 45" and "dFunction 46 through 99".
My Class is CDialog.
If you need anymore info Please ask, I really appreciate the help.
|
|
|
|
|
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 the extern 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:
<br />
void (*call_cmd_function[160])();<br />
void (*call_ins_function[48])();<br />
Then I defined them "globally" at the top of one of my source files as such:
<br />
void (CflowlogDlg::*call_ins_function[48])() = {NULL};<br />
void (CflowlogDlg::*call_cmd_function[160])() = {NULL};<br />
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:
<br />
CflowlogDlg INSFunction;<br />
INSFunction.call_ins_function[0] = &CflowlogDlg::doins00;<br />
INSFunction.call_ins_function[46] = &CflowlogDlg::doins5C;<br />
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
|
|
|
|
|
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 and call_cmd_function .
Are call_cmd_function and call_ins_function supposed to be a member of CflowlogDlg ?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Yes, both call_ins_function and call_cmd_function are members of CflowlogDlg.
|
|
|
|
|
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.
|
|
|
|
|
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:
<br />
static void (*call_cmd_function[160])();<br />
static void (*call_ins_function[48])();<br />
Then I define them at the top of the source files where I reference them:
<br />
void (CflowlogDlg::*call_cmd_function[160])() = {NULL};
void (CflowlogDlg::*call_ins_function[48])() = {NULL};
When I compile I get unresolved external symbol errors:
<br />
flowlogcmds.obj : error LNK2001: unresolved external symbol "public: static void (__cdecl** CflowlogDlg::call_cmd_function)(void)" (?call_cmd_function@CflowlogDlg@@2PAP6AXXZA)<br />
flowlogins.obj : error LNK2001: unresolved external symbol "public: static void (__cdecl** CflowlogDlg::call_ins_function)(void)" (?call_ins_function@CflowlogDlg@@2PAP6AXXZA)<br />
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 , and doins5C are all static 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
|
|
|
|
|
Thank you David. Finally got it working.
Thank you for all of the help
|
|
|
|
|
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
|
|
|
|
|
Your functions are public members. Are you initializing them in the .cpp file at the global scope or in a member function? If global, then I think that you are defining a new array that happens to have the same name as your member variable. You are not assigning values to your member array. Then when you call the function, the array is not initialized. Try this:
In your OnInitDialog():
// Init the functions pointers here
functionarray1[0] = CDialog::Func1 ;
functionarray1[1] = CDialog::Func2 ;
Later:
// call the functions
functionarray1[0]() ;
functionarray1[1]() ;
Make sure you declare Func1 and Func2 as static in CDialog.h
void (*functionarray1[48])();
void static Func1() ;
void static Func2() ;
Coadtoad
|
|
|
|
|
yes coadtoad, that is exactly what I am doing. I am initializing them at the global level in the .cpp file as such:
void (TMyClass::*funcArr2[48])() = {CDialog::func1,CDialog::func2};
I did it this way because I already know what functions I want in there, this is the way I did it in C and because it is less code to wirte then If i do it within a member function like initdialog.
Also is it a rule that the member functions have to be static?
|
|
|
|
|
If you want to have function pointers, I believe that the functions must be static, then you can't see the member variables from the static function. You may need to use a different approach like calling a CDialog member function that uses a switch statement to call the member functions. A little bit more messy, but you will have better encapsulation than writing a series of static functions.
Coadtoad
|
|
|
|
|
BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
CMyView* pView= (CMyView*)pFrame->GetActiveView();
pView->m_MyVar = 8;//<== Error occur here
//...........
}
How can I access variables in CMyView ?
Thanks in advance
|
|
|
|
|
What is the error you are getting? We are not mind readers (at least I'm not) and we can not see your screen, so you are going to have to give more infomation.
|
|
|
|
|
It appearces an error : Unhandled exception in ClipboardViewer.exe: 0xC0000005: Access Violation
|
|
|
|
|
The first *major* error that you're making is that you're not even verifying that the view pointer is valid. Since you're getting an access violation, it would appear that the view pointer is not valid. I would guess that you're calling GetActiveView in a state before the view is created.
|
|
|
|
|
I run my MDI project, in the first time, my program has one view, but when I click menu, the error occurres
Could you tell me how I can access variables in CMyView from CMainFrame
Thanks in advance
|
|
|
|
|
I found ways to get CMyView
(*)Get CMyView for SDI
+Method 1:
CMyView* pView = (CMyView*)AfxGetMainWnd();
+Method 2:
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
CMyView* pView = (CMyView*)pFrame->GetActiveView();
(*)Get CMyView for MDI
CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
CMDIChildWnd* pChild = (CMDIChildWnd*)pFrame->GetActiveFrame();
CMyView* pView = (CMyView*)pChild->GetActiveView();
;P
|
|
|
|
|
Hi, does anyone know how to use RLC? Can xplain how it is use? Can it be use to compress a DICOM image into a smaller file size? The purpose of reducing the size is to save space. Can it be done? Please help. Thank you.
|
|
|
|
|
Hi everyone,
I can't find any solution for my problem. I have application based on MFC App. The Doc/View architecture is off. After creating main frame I create main view window. Main view window creates two child windows (top part modeless dialog, and bottom part MSFlexGrid). The main part is top dialog since there will many others dialogs created and deleted dynamicaly. Grid is read only.
There is a basic toolbar and Edit menu.
The question is that how to enable (write?) message handling to pass cut, copy and paste to dialog. So picking (if enabled; now update command does not work so it is disabled) a copy from menu will copy text from dialof edit control and paste will paste data into control (which has focus).
I think this can be done by enabling correct message passing.. So it will be similar to doc/view mechanism. I tried with OnCmdMsg but I stuck on stack overflow.
I'm asking for help since I can't figure out any particular category to search in.
Thanks for any help.
regards,
slawek
Slawek
|
|
|
|
|
I have been looking for a simple free grid control to use in the view for a SDI-MFC-app. I want to show about 7 columns, of which one has editable cells. No databases.
In VS6, the best I could find was the MSFlexGrid ActiveX control.
In VS .Net I didn't find that control, but I found another grid control in the .NET-forms. I don't know how to use .NET-forms, so I don't want to use that one.
Any suggestions?
|
|
|
|
|
I have a function that recursively counts the number of files of a given type from the specified path.
It works as desired when used as a regualar function but I am trying to use this function in a worker thread that would return the number of files counted.
I have tried everything that I can think of but the results is always zero.
Here is the struct that I pass as a param to the threadproc
struct THREADSTRUCT
{
CString filename;
int count;
} ;
Here is my calling function.
int CPlayer::CountFiles( string path )
{
_paramCount->filename = path.c_str();
_paramCount->count = 0;
AfxBeginThread( Count, _paramCount );
return _paramCount->count;
}
Here is my threadproc
UINT Count( LPVOID pParam )
{
THREADSTRUCT* ts = (THREADSTRUCT*)pParam;
assert( !ts->filename.IsEmpty() );
int count = 0;
vector< string > files;
vector< string > dirs;
string path = ts->filename.GetBuffer();
string types[] =
{
".mp3",".mp2",".mp1",".ogg",".flac",".flc",".mpc",".ape",".ofr",".mpa",".wma"
};
int size = dim(types);
if( *(path.end() - 1) != '\\' && *(path.end() - 1) != '/' )
path += '\\';
string spec( path + "*.*" );
_finddata_t fd;
int handle = (int)_findfirst( spec.c_str(), &fd );
if( handle == -1 )
return 0;
do
{
if( fd.attrib & _A_SUBDIR )
{
if( fd.name != string(".") && fd.name != string("..") )
dirs.push_back( path + fd.name );
}
else
{
if( find_if( types, types+size, is_ext( fd.name ) ) != types+size )
{
files.push_back( path + fd.name );
count++;
ts->count++;
ct++;
pCPlayer->_paramCount->count++;
}
}
}while( !_findnext( handle, &fd ) );
_findclose(handle );
for( size_t i=0; i<dirs.size(); ++i )
{
pCPlayer->_paramCount->filename = dirs[i].c_str();
count += (int)Count( pCPlayer->_paramCount );
}
return count;
}
I have tried global variables and nothing seems to work. What am I missing?
|
|
|
|