Thread: Undeclared Identifier when trying to access an object

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    18

    Undeclared Identifier when trying to access 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?


    Object Array created here:
    Code:
    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
       }
    }
    edit:
    This is for Visual C++ .NET. It's a Windows Forms Application.

    Thanks for the help!
    Aaron
    Last edited by tineras; 05-06-2006 at 06:24 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    8
    picBlocks is a local variable to your menuFile_Click function. To be able to access it in another function, you'll have to either pass it as an argument, or put it in place where it is visible to both functions (Either global, or as a member variable if both functions are within the same class.)

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    I would prefer to make it a class member, but I'm not sure how to do that. I'm still learning and my teacher doesn't help us out very much.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    *bump*

    I just need to know how to add picBlocks[j] as a class member. If you need additional code accurately anser this question, I'd be glad to post it.

    Thanks

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    As a class member of what?

    What are you doing, overall? There could be a better solution to your general problem.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    8
    Class members are declared thusly:
    Code:
    class Foo
    {
    public:
       int myPublicMember;
    private:
       int myPrivateMember;
    };
    This will make myPublicMember and myPrivateMember available in any function of class Foo. So, if you want to do things this way, you'll need to figure out which object design encloses whichever functionality you're trying to program, and add your picBlocks variable in there. And since picBlocks is most obviously using Windows Forms, and that Windows Forms are C++/CLI, additional constraints may come to be.

    If your teacher isn't being helpful in the whole object/class design department, you're probably better off jsut making it a global variable for now until you more firmly grasp the basics of Object-oriented programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. Undeclared Identifier
    By Kayoss in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2006, 10:48 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Replies: 7
    Last Post: 03-10-2004, 04:10 PM