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

    Try with 2d vector catch the error

    I declare my 2d vector as follows:
    Code:
    std::vector<std::vector<int> > mattrix_
    in try-catch block and I receive the error caught from catch(...), I make this question just to confirm if it is of necessity for me to initialize that 2d vector at the same time in the try block...Hopefully I can receive any yes-or-nos or any comments from you...Sorry for such a question since I am really new to this.

    Thanks a lot in advance,

    Regards,

    -Milano
    Last edited by Milano; April 29th, 2004 at 03:29 PM.

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    Umm.. I don't think I understand what you're asking, but there's nothing wrong with the single line of code you posted. (Except from a missing ';' that is!) And certainly nothing that should end up in an exception being thrown.

    Could you please try to ******** what you are asking?

  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    Show us more of your code.

    Make sure you do a resize for each iteration.
    Example:
    PHP Code:
             std::vectorstd::vectorint > > vec_2dArray(Q_Col);
             for (
    int i 0;Q_Col;++ivec_2dArray[i].resize(Q_Row);
             for (
    int t 0;QtyTestItera;++t)
             for(
    int i0 0;i0 Q_Col;++i0)
             {
                  for(
    int i1 0;i1 Q_Row;++i1)
                  {
                       
    vec_2dArray[i0][i1] = i0 i1;
                  }
             } 
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  4. #4
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    I recommend using the following class for 2D arrays.

    PHP Code:
    template < class T>
    class 
    dynamic_2d_array
    {
    public:
        
    dynamic_2d_array(int rowint col):m_row(row),m_col(col), m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
        ~
    dynamic_2d_array(){if(m_datadelete []m_data;}
        
    inline Toperator[](int i) {return (m_data + (m_col*i));}
        
    inline T const*const operator[](int i) const {return (m_data + (m_col*i));}
    private:
        const 
    int m_row;
        const 
    int m_col;
        
    Tm_data
    }; 
    Declare it like this:
    PHP Code:
    dynamic_2d_array<intMy2DArray(3344); 
    You can then reference it just like a C-Style 2D array
    My2DArray[1][22] = 3;
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  5. #5
    Join Date
    Apr 2004
    Posts
    28
    Thanks a lot for your answers

    Actually, I'd like to use 2d vector to read the content of my file, but when I declared it as above (Sorry, I made a typo, without , my program couldnot read anything, I initialized it again and it worked fine then, thats why I made that question just for confirmation, thats all...

    Thanks again,

    -Milano

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