2d dynamic arrays

This is a discussion on 2d dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; please help me i cant seem to fix this program. i have 3 syntax errors and i dont know how ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    2d dynamic arrays

    please help me i cant seem to fix this program. i have 3 syntax errors and i dont know how to fix them. can someone show me how to do it plz?

    Code:
    #include <stdafx.h>
    #include <iostream>
    using namespace std;
     
    class TwoD
    {
        private:
            int rows;
            int cols;
            typedef int* IntArrayPtr;
        public :
            TwoD( );
            TwoD(int MaxRows, int MaxCols);
            void setValue(int row, int col, double value);
            double getValue(int row, int col);
            int getMaxRow();
            int getMaxCol();
            void destructure();
            int d1,d2,i,j,m[20][20];
    };
    
    TwoD::TwoD()
    {                                                             
        rows = 5;
        cols = 6;
        IntArrayPtr *m = new IntArrayPtr[rows];
        for (int i = 0; i,rows; i++)
            {
            m[i] = new int[cols];
            }
    }
    
    TwoD::TwoD(int maxRows,int maxCols)
    {
        rows = maxRows;
        cols=maxCols;
        IntArrayPtr *m = new IntArrayPtr[rows];
        for (int i= 0; i<rows; i++)
            {
            m[i]= new int[cols];
            }
    }
    
    void TwoD::setValue(int row, int col, double value)
    {
    m[row][col] = value;
    }
    double TwoD::getValue(int row, int col)
    {
        return m[row][col];
    }
    
    int TwoD::getMaxRow()
    {
        return rows;
    }
    
    int TwoD::getMaxCol()
    {
        return cols;
    }
    
    void TwoD::destructure ()
    {
        for (int i=0; i<d1; i++)
            {
            delete []m [i];
            delete []m;
            }
    }
    
    const TwoD TwoD::operator + (const TwoD&m, const TwoD&n) conts;          //     error C2039: '+' : is not a member of 'TwoD'         -          error C3646: 'conts' : unknown override specifier                      -            error C2447: '{' : missing function header (old-style formal list?)
    {
        int mRows=m.getMaxRows();
        int nRows=n.getMaxRows();
        int mCols=n.getMaxCols();
        int nCols=n.getMaxCols();
        if(mRows!=nRow||mCols!=nCols)
            {
            cout<< "Dimensions Not Equal :\n";
            return null:
            }
        TwoD sum(mRows,mCols)
       
        for(int i=0; i<mRows; i++)
            {
            for (int j=0; j<mCols; j++)
                {
                double mValue = getvalue(mRow,mCol);
                double nValue = getvalue(nRow,nCol);
                double mAndN = mValue + nValue;
                sum.setValue(mRows, mCols,mAndN);
                }
                return sum;
            }
    }
    
    int main ()
    {
        int d1,d2,i,j,value;
        cout << "Enter the row and column dimensions of the array:\n";
        cin >> d1 >> d2;
        TwoD matrix (d1,d2);
        cout << "Enter" <<d1<<"rows of" <<d2<< "integers each: \n";
        for (i=0; i<d1; i++)
            for (j=0; j<d2; j++)
                double value;
            cin>>value;
        matrix.setValue (d1, d2, value);
        cout<<"Echoing the two-dimensional array: \n";
        for (i=0; i<d1; i++)
        {
            for (j=0; j<d2; j++)
            cout<<matrix.getValue(d1,d2);
        }
        return 0;
    }
    thanks again and i also added where the errors are

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    two of those should be obvious errors. it's spelled const not conts. And what's the semi-colon doing afters const?

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    yah i already fixed that but im still getting two errors



    error C2039: '+' : is not a member of 'TwoD'
    error C2447: '{' : missing function header (old-style formal list?)

    agh i really need help

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    1) Look around! Do you see an operator + in the class definition? I certainly don't!
    2) You have a semicolon ; at the end of the function implementation. Remove it. The compiler is barking at that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the most important error is that your class does not have a member called m, they don't contain the array at all. Your constructors use a local variable m but this goes out of scope after the end of the constructor.

    typedef int* IntArrayPtr;
    I would think typing int* is simpler but...

    Code:
    void TwoD::destructure ()
    {
        for (int i=0; i<d1; i++)
            {
            delete []m [i];
            delete []m;
            }
    }
    Did you intend to delete the same memory over and over again?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    By the way, the destructor of a class should look like this:
    Code:
    TwoD::~TwoD()
    not this:
    Code:
    void TwoD::destructure ()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing 2D dynamic arrays
    By s_siouris in forum C Programming
    Replies: 5
    Last Post: 11-12-2008, 07:08 AM
  2. Seg Fault AND 2d dynamic arrays
    By lord in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2008, 01:07 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  5. Dynamic 2D arrays question
    By MadStrum! in forum C Programming
    Replies: 7
    Last Post: 02-08-2003, 04:54 AM

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