Arrays & Classes

This is a discussion on Arrays & Classes within the C++ Programming forums, part of the General Programming Boards category; Hi!I want to use an array as a class member.I wrote something like this: Code: class my_class{ public: .... int ...

  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 05: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,801
    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,328
    >> 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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Just wondering - It is C++ why not to use vector?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 11:13 AM
  2. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  3. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21