Undeclared identifer message
-
Hi, Why does the following code generate a compile error of C2065 in VC 6++
//Beginning of function
CStdioFile myFile; <-Why do I have to do this to avoid the compile error C2065 (Undeclared identifier)TRY
{
CStdioFile myFile((LPCTSTR)m_FilePath, CFile::modeRead | CFile:: typeText);
}
CATCH (CFileException, err)
{
MessageBox("Can't open file",...);
//Other lines to set focus to another control
}
END_CATCH//ASSERT(myFile.m_pStream!=NULL) <- Asserts here
Maui.File.SeekToBegin(); <-Asserts here due to Null referenceTo get the asserts I had to put a variable declaration of CStdioFile myFile at the top of the function. This is because of the error I get with "Undelcared identifier". I don't get it isn't it already being declared in the TRY...CATCH block? And why is it when I make a forward declaration it still asserts at the following two lines after END_CATCH? Really confused :confused: Please help. Sam C ---- Systems Manager Hospitality Marketing Associates
-
Hi, Why does the following code generate a compile error of C2065 in VC 6++
//Beginning of function
CStdioFile myFile; <-Why do I have to do this to avoid the compile error C2065 (Undeclared identifier)TRY
{
CStdioFile myFile((LPCTSTR)m_FilePath, CFile::modeRead | CFile:: typeText);
}
CATCH (CFileException, err)
{
MessageBox("Can't open file",...);
//Other lines to set focus to another control
}
END_CATCH//ASSERT(myFile.m_pStream!=NULL) <- Asserts here
Maui.File.SeekToBegin(); <-Asserts here due to Null referenceTo get the asserts I had to put a variable declaration of CStdioFile myFile at the top of the function. This is because of the error I get with "Undelcared identifier". I don't get it isn't it already being declared in the TRY...CATCH block? And why is it when I make a forward declaration it still asserts at the following two lines after END_CATCH? Really confused :confused: Please help. Sam C ---- Systems Manager Hospitality Marketing Associates
If you want to access myFile, it must be declared outside try/catch. And you should just use myFile in try/catch - currently you're creating another variable with identical name - and assert fires, because myFile visible outside of try/catch block is different than myFile you're playing with inside. BTW: why don't you use plain try/catch instead of MFC macros? Tomasz Sowinski -- http://www.shooltz.com
-
Hi, Why does the following code generate a compile error of C2065 in VC 6++
//Beginning of function
CStdioFile myFile; <-Why do I have to do this to avoid the compile error C2065 (Undeclared identifier)TRY
{
CStdioFile myFile((LPCTSTR)m_FilePath, CFile::modeRead | CFile:: typeText);
}
CATCH (CFileException, err)
{
MessageBox("Can't open file",...);
//Other lines to set focus to another control
}
END_CATCH//ASSERT(myFile.m_pStream!=NULL) <- Asserts here
Maui.File.SeekToBegin(); <-Asserts here due to Null referenceTo get the asserts I had to put a variable declaration of CStdioFile myFile at the top of the function. This is because of the error I get with "Undelcared identifier". I don't get it isn't it already being declared in the TRY...CATCH block? And why is it when I make a forward declaration it still asserts at the following two lines after END_CATCH? Really confused :confused: Please help. Sam C ---- Systems Manager Hospitality Marketing Associates
This has something to do with "scope" of the TRY block, i.e. TRY{...}. It's fine to use the variable inside the scope, i.e. inside the braces, but not outside of it (which you were doing in your ASSERT statement). Hope this helps. // Fazlul
Get RadVC today! Play RAD in VC++ http://www.capitolsoft.com
-
If you want to access myFile, it must be declared outside try/catch. And you should just use myFile in try/catch - currently you're creating another variable with identical name - and assert fires, because myFile visible outside of try/catch block is different than myFile you're playing with inside. BTW: why don't you use plain try/catch instead of MFC macros? Tomasz Sowinski -- http://www.shooltz.com
At first I did have try catch set up the old fashioned way.
try
{
CStdioFile myFile(...)
}
catch(CFileException *err)
{
TRACE("PROBLEM OCCURED");
err->Delete();
}...
But I was thinking that may have been part of my problem :-) MFC confuses me (as you can already tell). So basically I have to go back and Declare it outside and call the OPEN member function from within the try/catch block. Also is it bad to use MFC macros for MFC exceptions? I have never heard of a problem with this? Thanks for your quick response. Sam C ---- Systems Manager Hospitality Marketing Associates
-
This has something to do with "scope" of the TRY block, i.e. TRY{...}. It's fine to use the variable inside the scope, i.e. inside the braces, but not outside of it (which you were doing in your ASSERT statement). Hope this helps. // Fazlul
Get RadVC today! Play RAD in VC++ http://www.capitolsoft.com
So if I only wanted to check basically the "OPEN" function of the class I have declared I have to encapsulate the rest of the code within the try block? So for example:
...
try
{
Open(...)
//Rest of fuctions and command
}
catch (...)
{
}And I can't do this?
try
{
Open(...);
}
Catch (...)
{
}REST OF FUCTION CODE HERE USING CStdioFile
I find that peculiar and thanks for informing me of the try/catch scope, I did not know that it had it's own scope. Sam C ---- Systems Manager Hospitality Marketing Associates
-
So if I only wanted to check basically the "OPEN" function of the class I have declared I have to encapsulate the rest of the code within the try block? So for example:
...
try
{
Open(...)
//Rest of fuctions and command
}
catch (...)
{
}And I can't do this?
try
{
Open(...);
}
Catch (...)
{
}REST OF FUCTION CODE HERE USING CStdioFile
I find that peculiar and thanks for informing me of the try/catch scope, I did not know that it had it's own scope. Sam C ---- Systems Manager Hospitality Marketing Associates
You're right. This scope thingy is related to braces and has nothing to do with TRY-CATCH. You can get similar error with the following code:
{ int n = 0; } n = 5;
Hope this helps. // Fazlul
Get RadVC today! Play RAD in VC++ http://www.capitolsoft.com