CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2006
    Posts
    31

    Undeclared Identifier when trying to use an object

    I created an object array when a button is clicked. When I try to use any of the objects in another function, it is saying that it's an undeclared identifier.

    What do I need to do to make sure I can use 'picBlocks[k]' anywhere else in my code?



    Code:
    Object Array created here:
    public: System::Void menuFile_Click(...)
    {
          PictureBox* picBlocks[] = new PictureBox*[5];
    
          for(int i = 0; i < 5; i++)
          {
            picBlocks[i] = new PictureBox();
            pnlGameField->Controls->Add(picBlocks[i]);
          }
    }
    
    Trying to access object here in a different function:
    bool OtherFunction()
    {
       if(picBlocks[0] = xxxx) <---says picBlocks is undeclared
       {
          Blah Blah Blah
       }
    }
    I still new to programming, so the more details, the better. I was told previously that I needed to add picBlocks[] to a class. I don't really know how to do that.

    Thanks for the help!
    Aaron
    Last edited by Tineras; May 7th, 2006 at 03:08 PM.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Undeclared Identifier when trying to use an object

    What do I need to do to make sure I can use 'picBlocks[k]' anywhere else in my code?
    Declare the variable outside of the functions body:
    Code:
    class YourClass
    {
    public:
        // declared at class scope (can be accessed from all member functions)
        int i;
    
        void functionA()
        {
            i = 5; // ok
    
            int j; // declared in function scope, can only be accessed from this function
            j = 5; // ok;
        }
    
        void functionB()
        {
            i = 5; // ok
    
            j = 5; // not ok!
        }
    };
    - petter

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Undeclared Identifier when trying to use an object

    What about reading a good C++ book?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured