Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Undeclared identifer message

Undeclared identifer message

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionsales
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sam C
    wrote on last edited by
    #1

    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 reference

    To 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

    T F 2 Replies Last reply
    0
    • S Sam C

      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 reference

      To 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

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • S Sam C

        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 reference

        To 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

        F Offline
        F Offline
        Fazlul Kabir
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • T Tomasz Sowinski

          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

          S Offline
          S Offline
          Sam C
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • F Fazlul Kabir

            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

            S Offline
            S Offline
            Sam C
            wrote on last edited by
            #5

            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

            F 1 Reply Last reply
            0
            • S Sam C

              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

              F Offline
              F Offline
              Fazlul Kabir
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups