Thread: help with classes

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    help with classes

    Im trying to put constants in my class and i get wierd compile messages saying only "=0" allowed. Here is how im doing it.

    class Minesweeper {
    public:
    const int ROW = 9;
    ....

    Am i suppose to define constants out of a class. Also can I define a structure and then use it. I want to define a structure called cell then make an array of "cell"s but I also get errors. I didnt include any header files in this .h. Do i need the iostream header to do basic tasks?
    Im use to java programming and classes seem real old fashioned in c++ hehe. Thanks

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you are trying to declare a variable and initialize it at the same time while inside a class.. not good. Instead, declare your variable and then initialize it using a constructor.

    Code:
    class Classy
    {
    public:
         
         Classy(){foo=0;}  //Inline function for this example
    
    private:
    
         int foo;
    };
    • "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
    You can have constant data members but they must be initialized in an initializer list:

    Code:
    class Minesweeper
    {
    public:
        const int ROW;
        const int COL;
        Minesweeper() : ROW(9), COL(9)  // This part here is the initializer list
        {
            // Rest of constructor code goes here
        }
    };
    "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
    May 2005
    Posts
    6
    ok so i can do this
    Code:
    CMinesweeper::CMinesweeper() : ROW(9), COL(9) {}
    ok can i do this
    Code:
    public:
    struct Cell { blah blah};
    Cell board[ROW][COL];
    1st question can i initialize a structure and array like this.
    2nd since row and col arent initialized a number until i do the constructor code in the .cpp file will there be an error. How can i have constants already initialized, should i stick with #defines...C# makes this much easier.
    Sorry for these noob questions...im use to C#/Java techniques

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You might want to consider a templated class, you can even have a default grid size if necessary. As an example:

    Code:
    template <int row=9,int col=9>
    class Minesweeper
    {
    public:
        const int ROW;
        const int COL;
        int grid[row][col];
        Minesweeper():ROW(row),COL(col) {}
    };
    
    ...
    
    // Create a default minesweeper grid w/ 9 rows, 9 cols
    Minesweeper<> easy;
    
    // Create another minesweeper grid w/ 20 rows, 20 cols
    Minesweeper<20,20> hard;
    "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

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    6
    Im having problems still here is part of my class declaration
    Code:
    class CMinesweeper {
    	public:
    		const int MAXBOMBS;
    		const int ROW;
    		const int COL;
    		const char BOMB;
    		const char EMPTY;
    		const char MARKER;
    
    		CMinesweeper();
    		~CMinesweeper();
                  private:
    		struct Cell {
    			int value;
    			bool show;
    			void show() { show = true; }
    		};
    		Cell board[ROW][COL];
    The class constructor is
    Code:
    CMinesweeper::CMinesweeper(): ROW(9), COL(9), MAXBOMBS(9), BOMB('*'), EMPTY(' '), MARKER('|')  {...}
    I get a lot of errors stating the initializers arent initalized and therefore cant create the array.
    For the structure error C2040: 'show' : 'void (void)' differs in levels of indirection from 'bool'. Theres a differennce a bool variable called show and a function that makes show always true

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Make the constants static:
    Code:
    class Constants
    {
      static const int FOO = 9;
      static const int BAR = 2310;
    };
    Note that this will not compile on some older compilers, because it was a rater late addition to the standard. The usual workaround is to use an enum:
    Code:
    class Constants
    {
      enum { FOO = 9 };
      enum { BAR = 2310 };
    };
    The Boost libraries provide a common form with the macro BOOST_STATIC_CONSTANT(Type, assignment). From their description:
    onstant members, we must use enums as a workaround if we want the constants to be available at compile-time. This macro gives us a convenient way to declare such constants. For example instead of:
    Code:
    struct foo{
       static const int value = 2;
    };
    use:
    Code:
    struct foo{
       BOOST_STATIC_CONSTANT(int, value = 2);
    };
    And there's more. Static member constants, even when in-class initialized, still require definitions in exactly one source file. The enums don't. This means you somehow have to decide whether to put the definition there. This is done using the BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro:
    Code:
    #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
    const int Constants::FOO;
    const int Constants::BAR;
    #endif
    Note that these definitions do not have an initializer.

    Quote Originally Posted by Last Public Draft of the C++ Standard, §9.4.2/4
    If a static data member is of const integral or const enumeration
    type, its declaration in the class definition can specify a constant-
    initializer which shall be an integral constant expression
    (_expr.const_). In that case, the member can appear in integral con-
    stant expressions within its scope. The member shall still be defined
    in a namespace scope if it is used in the program and the namespace
    scope definition shall not contain an initializer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM