Thread: Arrays & Classes

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Question Arrays & Classes

    Hi!I want to use an array as a class member.I wrote something like this:
    Code:
    class my_class{
          
             public:
                      ....
                      int x;
                      int array[5];
                      void function();          
                       .....
             }
    
    my_class::my_class()
    {
            ....
             x = 0;
             array[ 5 ] = { 0,1,2,3,4 };
    }
    
    void my_class::function()
    {
         for( i = 0; i < 5; i++ )
         {
               if ((something) > array[i] )
               {
                     do something.....;
                }
          }
    }
    The compiler (Dev-C++) gave an error with the message "expected priimary - expression before '{' token : in the line // array[ 5 ] = { 0,1,2,3,4 }; // of the constructor.

    Any ideas...?

    MaRaDoNa
    ========
    Last edited by maradona; 12-22-2006 at 06:29 PM.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    thou shalt not initialize variables inside of a class.. that is the sole responsibility of the class constructor.

    Code:
     x = 0;    <- not allowed inside a class
    array[ 5 ] = { 0,1,2,3,4 };    <- not allowed inside a class
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    array[5] does not exist... array's are 0-based so valid indicies go from 0 up to and including 4 (but not 5). In addition, array[5], if it existed, would be a single element of the array to which you are attempting to assign 5 integers. I'd suggest a loop:

    Code:
    my_class::my_class()
    {
        ....
        x = 0;
        for( int i = 0; i < 5; ++i )
            array[ i ] = i;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> x = 0; <- not allowed inside a class
    That is allowed, it is just an assignment instead of an initialization.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    youre missing a semicolon after the class declaration

    >>that is the sole responsibility of the class constructor.
    it is in the constructor

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    x=0; is perfectly allowable inside a constructor, the problem is that inside the body of a constructor it's assignment, rather than construction. the correct way of doing this is ctor() : x(0), ... {} here x is constructed with the value 0. As to constructing an array with a list of values I am afraid you are simply out of luck. std::vectors can be constructed with N copies of any value you like. However, if you want each value in the array to be different you need to copy them all in. an ugly but workable solution would be
    Code:
    my_class::my_class() : x(0) 
    {
            int dummy[] = { 0,1,2,3,4 };
            memcpy(array,dummy,sizeof(array)/sizeof(array[0]));
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Just wondering - It is C++ why not to use vector?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic allocation of 2 dim. arrays in classes
    By circuitbreaker in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2008, 12:13 PM
  2. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  3. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 06:49 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM