Initialising a 2D vector in a constructor!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssmny
    New Member
    • Jul 2010
    • 7

    #1

    Initialising a 2D vector in a constructor!

    Hello all there!
    I have no experience with STL but now i must use it.
    I wanna do this:
    I have a class "Token" in the "tokenClass .h" and

    1)I wanna be eble initialise my *m_Board in a default constructor.

    2)and I want also to be eble to initialise my m_Board Dynamicaly

    ... anyone there to help me I will be very greatifull

    Code:
    #include "tokenClass.h"
    #include <vector>
    using namespace std;
    
    class GameB
    {
    private:
        vector<vector<Token> >* m_Board;
        int xloc;
        int yloc;
    public:
        GameB()
        {
            m_Board = new vector<vector<Token> >(6,vector<Token>(6,0));
        }
    
        GameB(int locatx,int locaty)
            :xloc(locatx),yloc(locaty)
        {
        m_Board=new vector<vector<Token> >(xloc,vector<Token>(yloc));
        }
    
    };
    Last edited by Banfa; Jul 15 '10, 11:12 AM. Reason: Added [code]...[/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You haven't actually said what the problem you are getting is, does this compile?

    Comment

    • ssmny
      New Member
      • Jul 2010
      • 7

      #3
      Actually, considering only the default constructor, if the elements were integers(instea d of "Tokens") it would compile, but if the vector is to take "Token" as it is there, it does not.
      I suspect, it has something to do with overloading the "=" although the compile doesn't tell that.
      2) with the second construct, its seems all wrong. so i need your Help please!
      Thanks!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        What are the compilation errors?

        Comment

        • ssmny
          New Member
          • Jul 2010
          • 7

          #5
          error: no matching function for call to 'std::vector<To ken, std::allocator< Token> >::_M_fill_init ialize(size_t, int&)'

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            On which line number in the code? Please post all the relevant information.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Probably here

              Code:
                      m_Board = new vector<vector<Token> >(6,vector<Token>(6,0));
              0 is not a Token, you need to provide a Token e.g. Token() for the default constructed token

              Comment

              • ssmny
                New Member
                • Jul 2010
                • 7

                #8
                Banfa: u are great man, thanks a lot.
                I am now planning to better the non default constructor
                I want to be putting these tokens into that m_Board.
                If you have the idea how i should do it, please dont hasitate to let me know. Otherwise I try my best and turn to in sometime.

                Comment

                • ssmny
                  New Member
                  • Jul 2010
                  • 7

                  #9
                  But a small stupid question please:
                  (m_Board = new vector<vector<T oken> >(6,vector<Toke n>(6,0));)
                  What does that whole statement mean?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Also, you don't need a vector pointer m_board.

                    Just make m_board a vector. A vector keeps all of its data on the heap anyway.

                    If you need to change boards then just erase the current vector from begin() to end().

                    Comment

                    Working...