CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Posts
    11

    simple constructor question

    I'm writing a simple game application. A game object is composed of a Board and an AI player. When the AI player is constructed it needs a reference to a board object, so it can interact with it. How can I set up the game class so that its members get constructed properly--considering that one needs a reference to another?

    Code:
    class Game
    {
        private:
            Board theBoard;
            AI computerPlayer;  // AI needs reference to theBoard (above)
    };
    
    class AI
    {
        AI(Board& board_in) : theBoard(board_in)
    
        private:
            Board& theBoard;
    };
    Last edited by mjl3434; June 28th, 2010 at 02:36 AM.

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: simple constructor question

    Objects are constructed in the order they are declared, so could you do:


    Game() : computerPlayer(theBoard) {
    }


    Or alternatively, have an initialisation function that takes a Board ?
    your humble savant

  3. #3
    Join Date
    Dec 2009
    Posts
    11

    Re: simple constructor question

    When I try this there is a scope problem. I ran a simple test:

    Code:
    #include <iostream>
    
    using std::cout;
    
    class ObjectA;
    class ObjectB;
    
    class ObjectB
    {
       public:
            ObjectB(ObjectA& refIn) : associatedObjectA(refIn)
            {
                cout << "Object B constructor.\n";
            }
    
        private:
            ObjectA& associatedObjectA;
    };
    
    class ObjectA
    {
        public:
            ObjectA()
            {
                cout << "Object A constructor.\n";
            }
    };
    
    class Container
    {
        public:
            Container() : theObjectB(theObjectA)
        private:
            ObjectA theObjectA;
            ObjectB theObjectB;
    };
    
    int main()
    {
        Container myContainer;
        return 0;
    }
    And the error I get is:

    t.cpp: In constructor 'Container::Container()':
    Line 32: error: 'theObjectA' was not declared in this scope
    compilation terminated due to -Wfatal-errors.


    Is it just that what I am doing does not make sense or is not possible?

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: simple constructor question

    The body for container constructor is missing.
    Quote Originally Posted by mjl3434 View Post
    When I try this there is a scope problem. I ran a simple test:

    Code:
    #include <iostream>
    
    using std::cout;
    
    class ObjectA;
    class ObjectB;
    
    class ObjectB
    {
       public:
            ObjectB(ObjectA& refIn) : associatedObjectA(refIn)
            {
                cout << "Object B constructor.\n";
            }
    
        private:
            ObjectA& associatedObjectA;
    };
    
    class ObjectA
    {
        public:
            ObjectA()
            {
                cout << "Object A constructor.\n";
            }
    };
    
    class Container
    {
        public:
            Container() : theObjectB(theObjectA)
        private:
            ObjectA theObjectA;
            ObjectB theObjectB;
    };
    
    int main()
    {
        Container myContainer;
        return 0;
    }
    And the error I get is:

    t.cpp: In constructor 'Container::Container()':
    Line 32: error: 'theObjectA' was not declared in this scope
    compilation terminated due to -Wfatal-errors.


    Is it just that what I am doing does not make sense or is not possible?

  5. #5
    Join Date
    Dec 2009
    Posts
    11

    Re: simple constructor question

    Yes that's right. I got it working now. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured