Thread: Problem with classes and Arrays

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Question Problem with classes and Arrays

    Hello i'm a newbie, i've done all the basic tutorials but i am confused about the classes;

    what i want to do is make the game exploding atoms.
    it requires a field of 10x10 and 2 deep to combine values.

    I want this all in a class called playfield and i want to have a function called clrfield to clear the playfield

    This is what I've tried so far:

    Code:
    class playfield 
    {
          public:
                 playfield();
                 ~playfield();
                 int field[10][10][2];
                 int expnumber;
                 
    
    };
    playfield::playfield()
    {
     
                          }
    playfield::~playfield()
    {
                           }
    
    void clrfield()
    { 
         int a, b;
         for (a=0;a<=10;a++){
             for (b=0;b<=10;b++){
                 playfield::field[a][b][1]=0;
                 }
                 }
    }
    what did i do wrong?
    thnx in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be clearing both "playfield::field[a][b][0]" and "playfield::field[a][b][1]". Also, 'clrfield' should be a member of the playfield class.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    I get the error: 'playfield::field' is non-static but is referred to as static

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its because you don't have an instance of playfield in your function. There are two ways around this depending on how exactly you want to implement your code. One is to make clearfield a member function of the class (preferred method):
    Code:
    class playfield 
    {
          public:
                 playfield();
                 ~playfield();
                 void clrfield();
                 int field[10][10][2];
                 int expnumber;
                 
    
    };
    playfield::playfield()
    {
     
                          }
    playfield::~playfield()
    {
                           }
    
    void playfield::clrfield()
    { 
         int a, b;
         for (a=0;a<=10;a++){
             for (b=0;b<=10;b++){
                 field[a][b][1]=0;
                 }
                 }
    }
    Or if you intend for the function to not be a part of the class then you have to declare an instance of playfield in your function like you would any other variable type like int, char, etc (not-preferred):
    Code:
    class playfield 
    {
          public:
                 playfield();
                 ~playfield();
                 int field[10][10][2];
                 int expnumber;
                 
    
    };
    playfield::playfield()
    {
     
                          }
    playfield::~playfield()
    {
                           }
    
    void clrfield()
    { 
         int a, b;
         playfield myPlayfield;
         for (a=0;a<=10;a++){
             for (b=0;b<=10;b++){
                 myPlayfield.field[a][b][1]=0;
                 }
                 }
    }
    This last variation is not preferable since its usually a bad idea to allow the user direct access to the data of a class.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    thanks that helped a lot!!
    I am now beginning to understand how classes work
    the tutorial wasnt explaining enough but now i know how it works thanx

    greetings GonzO

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help on this problem using classes and arrays
    By john5754 in forum C++ Programming
    Replies: 21
    Last Post: 11-30-2008, 06:35 PM
  2. problem in classes and arrays
    By hiya in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2005, 02:45 PM
  3. Arrays with base/derived classes
    By shaeng in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 11:54 AM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM
  5. Help with Arrays of Classes
    By GrNxxDaY in forum C++ Programming
    Replies: 15
    Last Post: 07-25-2002, 09:40 PM