Like Tree1Likes
  • 1 Post By brewbuck

dynamic 2d-array

This is a discussion on dynamic 2d-array within the C++ Programming forums, part of the General Programming Boards category; Hi All, While running the below code i am getting Error like :- In member function ‘void Matrix::get_element(int, int, int)’: ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    5

    dynamic 2d-array

    Hi All,

    While running the below code i am getting
    Error like :-

    In member function ‘void Matrix::get_element(int, int, int)’:
    cons_2d_array.cpp:13:9: error: invalid types ‘int[int]’ for array subscript
    cons_2d_array.cpp: In member function ‘int& Matrix: put_element(int, int)’:
    cons_2d_array.cpp:16:23: error: invalid types ‘int[int]’ for array subscript
    cons_2d_array.cpp: In constructor ‘Matrix::Matrix(int, int)’:
    cons_2d_array.cpp:23:18: error: cannot convert ‘int**’ to ‘int*’ in assignment
    cons_2d_array.cpp:25:28: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]

    Code:
    
    class Matrix
    {
            int *p;
            int d1,d2;
        public:
            Matrix()
            {d1=0;d2=0;}
            Matrix(int x,int y);
    
    
            void get_element(int i, int j, int value)
            {p[i][j]=value;}
    
    
            int & put_element(int i, int j)
            {return p[i][j];}
    };
    
    
    Matrix :: Matrix(const int x, const int y)
    {
            d1=x;
            d2=y;
            p = new int *[d1];
            for(int i=0; i<d1; i++)
               p[i]= new int[d2];
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,828
    Stop. Have you considered the use of a std::vector instead of manual memory management of a dynamic array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,354
    So, if I want to get the value of an element, I use put_element(), and if I want to set the value I use get_element()?
    laserlight likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    33,292
    Assuming this is some exercise (use vectors as laserlight says, if this is real code), your member variable needs to be

    Code:
            int **p;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Thanks for advice.
    I am doing R&D with c++ that's why i asked this question.
    Please help me to solve this

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Thanks,
    I was really missing this part int **p

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic 2D array
    By Niels_M in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2012, 03:01 PM
  2. Replies: 10
    Last Post: 12-03-2011, 01:26 PM
  3. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  4. I want a dynamic array.
    By MK27 in forum C Programming
    Replies: 10
    Last Post: 08-27-2008, 11:47 PM
  5. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM

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