Undeclared identifier
-
I´ve created an edit box Member Variable and I have to use it from another file but I don't know how could I do it. In my tree view I have both files but, how could I use my m_strVar from the main file in the other one, so that I could update the edit box from this last file.cpp? Thank you in advance :) I have no idea :doh:
-
I´ve created an edit box Member Variable and I have to use it from another file but I don't know how could I do it. In my tree view I have both files but, how could I use my m_strVar from the main file in the other one, so that I could update the edit box from this last file.cpp? Thank you in advance :) I have no idea :doh:
How about
extern
?
A rich person is not the one who has the most, but the one that needs the least.
-
How about
extern
?
A rich person is not the one who has the most, but the one that needs the least.
-
How could I use this? If I write extern m_strVal; in my main file .cpp I'd get a redefinition, wouldn't I? Thank you! I have no idea :doh:
satcat wrote: If I write extern m_strVal; in my main file .cpp I'd get a redefinition, wouldn't I? Per the documentation: The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s). In other words, no, you would not get a redefinition error.
A rich person is not the one who has the most, but the one that needs the least.
-
satcat wrote: If I write extern m_strVal; in my main file .cpp I'd get a redefinition, wouldn't I? Per the documentation: The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s). In other words, no, you would not get a redefinition error.
A rich person is not the one who has the most, but the one that needs the least.
-
I´ve created an edit box Member Variable and I have to use it from another file but I don't know how could I do it. In my tree view I have both files but, how could I use my m_strVar from the main file in the other one, so that I could update the edit box from this last file.cpp? Thank you in advance :) I have no idea :doh:
If it is a member variable of a class the extern keyword does not apply, extern applies to globaly declared (non-static) variables. If the class variable is declared grobaly, then the extern keyword may be applied to the class variable (giving you access to all public members of that class). If the member variable contains the same information no matter how many instances of the class are created, then you might as well make it a static member. In which case you can modify it even if no intances of the class exist yet. (Example: CMyClass::m_strVar = _T("new data");) If you do not want to make it a public member but still want to access the member directly (without use member functions), then make the class that needs direct access a friend of the class containing them member variable. INTP
-
If it is a member variable of a class the extern keyword does not apply, extern applies to globaly declared (non-static) variables. If the class variable is declared grobaly, then the extern keyword may be applied to the class variable (giving you access to all public members of that class). If the member variable contains the same information no matter how many instances of the class are created, then you might as well make it a static member. In which case you can modify it even if no intances of the class exist yet. (Example: CMyClass::m_strVar = _T("new data");) If you do not want to make it a public member but still want to access the member directly (without use member functions), then make the class that needs direct access a friend of the class containing them member variable. INTP
I have no idea about this... Perhaps if I try to explain you with some code cuts you could help me: In my main code1.cpp I have defined a edit box with a member variable. In my code it appears on this way:
CIVOCOMDlg::CIVOCOMDlg(CWnd* pParent /*=NULL*/) : CDialog(CIVOCOMDlg::IDD, pParent) { //{{AFX_DATA_INIT(CIVOCOMDlg) m_strHeard = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CIVOCOMDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CIVOCOMDlg) DDX_Text(pDX, IDC_GOT, m_strHeard); //}}AFX_DATA_MAP }
Then in this code1.cpp I create a Grammar object:BOOL loadGrammar( TCHAR *pszFile, BOOL bDictGram, ISRGramCommon **ppISRGramCommon ) { ... // load the grammar and register our notify sink IUnknown * pIUnkGram; // create dictation grammar notify sink // CGramDictSink *pGramDictSink = new CGramDictSink( hDlg ); CGramDictSink *pGramDictSink = new CGramDictSink(NULL); pGramDictSink->AddRef(); hRes = g_pISRCentral->GrammarLoad( SRGRMFMT_DICTATION, sData, pGramDictSink, IID_ISRGramNotifySink, &pIUnkGram ); pGramDictSink->Release(); ... hRes = pIUnkGram->QueryInterface( IID_ISRGramCommon,(void **)ppISRGramCommon ); pIUnkGram->Release(); hRes = (*ppISRGramCommon)->Activate( NULL, FALSE, NULL ); return SUCCEEDED( hRes ); }
In the code2.cpp file I have:// notifies client that a recognition has completed STDMETHODIMP CGramCFGSink::PhraseFinish( DWORD dwFlags, QWORD /*qTimeStampBegin*/, QWORD /*qTimeStampEnd*/, PSRPHRASE pSRPhrase, LPUNKNOWN /*lpResults*/ ) { // ignore if not a recognition from this grammar if( !( dwFlags & ISRNOTEFIN_RECOGNIZED ) || !( dwFlags & ISRNOTEFIN_THISGRAMMAR ) ) { return S_OK; } // retrieve the recognized text from SRPHRASE structure PSRWORD pSRMax = (PSRWORD)( (BYTE*)pSRPhrase + pSRPhrase->dwSize ); PSRWORD pSRWord = (PSRWORD)( pSRPhrase->abWords ); TCHAR szText[256] = { _T('\0') }; int nWords = 0; while( pSRWord < pSRMax ) { // add a space between words if( nWords++ != 0 ) _tcscat( szText, _T(" ") ); _tcscat( szText, pSRWord->szWord ); pSRWord = (PSRWORD)( (BYTE *)pSRWord + pSRWord->dwSize ); } SetDlgIt
-
I have no idea about this... Perhaps if I try to explain you with some code cuts you could help me: In my main code1.cpp I have defined a edit box with a member variable. In my code it appears on this way:
CIVOCOMDlg::CIVOCOMDlg(CWnd* pParent /*=NULL*/) : CDialog(CIVOCOMDlg::IDD, pParent) { //{{AFX_DATA_INIT(CIVOCOMDlg) m_strHeard = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CIVOCOMDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CIVOCOMDlg) DDX_Text(pDX, IDC_GOT, m_strHeard); //}}AFX_DATA_MAP }
Then in this code1.cpp I create a Grammar object:BOOL loadGrammar( TCHAR *pszFile, BOOL bDictGram, ISRGramCommon **ppISRGramCommon ) { ... // load the grammar and register our notify sink IUnknown * pIUnkGram; // create dictation grammar notify sink // CGramDictSink *pGramDictSink = new CGramDictSink( hDlg ); CGramDictSink *pGramDictSink = new CGramDictSink(NULL); pGramDictSink->AddRef(); hRes = g_pISRCentral->GrammarLoad( SRGRMFMT_DICTATION, sData, pGramDictSink, IID_ISRGramNotifySink, &pIUnkGram ); pGramDictSink->Release(); ... hRes = pIUnkGram->QueryInterface( IID_ISRGramCommon,(void **)ppISRGramCommon ); pIUnkGram->Release(); hRes = (*ppISRGramCommon)->Activate( NULL, FALSE, NULL ); return SUCCEEDED( hRes ); }
In the code2.cpp file I have:// notifies client that a recognition has completed STDMETHODIMP CGramCFGSink::PhraseFinish( DWORD dwFlags, QWORD /*qTimeStampBegin*/, QWORD /*qTimeStampEnd*/, PSRPHRASE pSRPhrase, LPUNKNOWN /*lpResults*/ ) { // ignore if not a recognition from this grammar if( !( dwFlags & ISRNOTEFIN_RECOGNIZED ) || !( dwFlags & ISRNOTEFIN_THISGRAMMAR ) ) { return S_OK; } // retrieve the recognized text from SRPHRASE structure PSRWORD pSRMax = (PSRWORD)( (BYTE*)pSRPhrase + pSRPhrase->dwSize ); PSRWORD pSRWord = (PSRWORD)( pSRPhrase->abWords ); TCHAR szText[256] = { _T('\0') }; int nWords = 0; while( pSRWord < pSRMax ) { // add a space between words if( nWords++ != 0 ) _tcscat( szText, _T(" ") ); _tcscat( szText, pSRWord->szWord ); pSRWord = (PSRWORD)( (BYTE *)pSRWord + pSRWord->dwSize ); } SetDlgIt
First: CGramDictSink *pGramDictSink = new CGramDictSink(NULL); // Make sure pGramDictSink is not NULL // If it is clean up and exit with failure. Secound: m_strHeard does not seem to be needed, unless the string contained in it needs to be accessed after the dialog is closed or by some other class if the dialog is not modal. Third: "Unrecognized\0" is equivalent to the folowing: char str[14] { 'U','n','r','e','c','o','g','n','i','z','e','d','\0','\0' }; You sould use "Unrecognized". Forth: Just incase you ever want to compile your code as UNICODE you should declare your strings as follows: _T("Unrecognized") Otherwise: Using function macros like _tcscat() does not make since. Fith: The line "TCHAR szText[256] = { _T('\0') }" looks dangerious. How can you be absolutly sure that the final string will not exceed the size of the buffer. Use CString instead (if using MFC), it will dinamicaly icrease the size of the buffer as needed. Sixth: As for why SetDlgItemText(m_hDlg, IDC_GOT, "Unrecognized\0") does not work. I do not know, if the dialog handle and control ID are valid it should work. Of cource you could be overrunning the buffer mentioned above, which may resault in your problem (I dought it). Or if the code presented here is not part of the dialog derived class and the dialog is modal, then the function call will only occur when the dialog is closed, in which case the dialog handle will be invalid. Seventh: It looks like you need to add more error checking to your code. Eighth: In the future when you post code, use
before the code and
after the code. That way the we will not have to reformat it, to see what is happening. Finaly: I appoligise, if I sound abrubt, but this does not seem to apply to the originaly question at all. INTP
-
First: CGramDictSink *pGramDictSink = new CGramDictSink(NULL); // Make sure pGramDictSink is not NULL // If it is clean up and exit with failure. Secound: m_strHeard does not seem to be needed, unless the string contained in it needs to be accessed after the dialog is closed or by some other class if the dialog is not modal. Third: "Unrecognized\0" is equivalent to the folowing: char str[14] { 'U','n','r','e','c','o','g','n','i','z','e','d','\0','\0' }; You sould use "Unrecognized". Forth: Just incase you ever want to compile your code as UNICODE you should declare your strings as follows: _T("Unrecognized") Otherwise: Using function macros like _tcscat() does not make since. Fith: The line "TCHAR szText[256] = { _T('\0') }" looks dangerious. How can you be absolutly sure that the final string will not exceed the size of the buffer. Use CString instead (if using MFC), it will dinamicaly icrease the size of the buffer as needed. Sixth: As for why SetDlgItemText(m_hDlg, IDC_GOT, "Unrecognized\0") does not work. I do not know, if the dialog handle and control ID are valid it should work. Of cource you could be overrunning the buffer mentioned above, which may resault in your problem (I dought it). Or if the code presented here is not part of the dialog derived class and the dialog is modal, then the function call will only occur when the dialog is closed, in which case the dialog handle will be invalid. Seventh: It looks like you need to add more error checking to your code. Eighth: In the future when you post code, use
before the code and
after the code. That way the we will not have to reformat it, to see what is happening. Finaly: I appoligise, if I sound abrubt, but this does not seem to apply to the originaly question at all. INTP
Thank you for your help... I'm a newbie and I have a very little bit idea about programming in Visual C++, and so I got lots of mistakes in my message. My problem is that I can't write in the main window edit box from the gramsink.cpp code. That's the reason why I wrote both codes because it's the only thing doens't work in the application. If I write a MessageBox I get in a new window what I want to be written in the main window so the code works. If i use m_strHeard in gramsink.cpp I get theerror message "Undeclared identifier". This is what I meant with my prior question. I only wanted a method so I could write in the existing window Edit Box and not in a new window. Using extern or other way (I don't know). Sorry if you've got disturbed with my greenness. Regards.