Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,668 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,150 people online right now. Registration is fast and FREE... Join Now!




2dsafearray

 
Reply to this topicStart new topic

2dsafearray, error in 2dsafearray

freshman_prog
post 4 Sep, 2007 - 10:08 AM
Post #1


New D.I.C Head

*
Joined: 23 Mar, 2007
Posts: 23


My Contributions


i've get errors which i can't remove plz help......



CODE

#include<iostream>
using namespace std;

class twod
{
      private :
              int *data;
              int col;
              int row;
      public :
             twod() : data(0) , col(0) , row(0) {}
             twod(const int r ,const int c)
             {
                      row = r;
                      col = c;
                      data = new int [(row * col)];
             }
             twod(const twod &rhs)//ASSIgnment OPERAtor
             {
                        row = rhs.row;
                        col = rhs.col;
                        if (data)
                        delete[] data;
                        data = new int [(row * col)];
                        memcpy(data,rhs.data,sizeof(int) * row * col);
             }
             twod operator = (const twod &rhs)
             {
                            row = rhs.row;
                            col = rhs.col;
                            if (data)
                            delete[] data;
                            data = new int [row * col];
                            memcpy(data,rhs.data,sizeof(int) * row * col);
             }
             int& operator () (int r , int c)
             {
                  if (r < row && r > 0 && c < col && c > 0)
                  {
                        int temp = (*data) * r + c;
                        return temp;
                  }
             }
};
int main()
{
twod a , b(2,3);
/*for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
        b(i,j) = (0);
}
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
        cout << b(i, j) << endl;
}

system("pause");  
return 0;
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 4 Sep, 2007 - 10:09 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Please post the errors you are receiving.
User is online!Profile CardPM

Go to the top of the page

freshman_prog
post 4 Sep, 2007 - 10:13 AM
Post #3


New D.I.C Head

*
Joined: 23 Mar, 2007
Posts: 23


My Contributions


QUOTE(Amadeus @ 4 Sep, 2007 - 11:09 AM) *

Please post the errors you are receiving.

CODE

#include<iostream>
using namespace std;

class twod
{
      private :
              int *data;
              int col;
              int row;
      public :
             twod() : data(0) , col(0) , row(0) {}
             twod(const int r ,const int c)
             {
                      row = r;
                      col = c;
                      data = new int [(row * col)];
             }
             twod(const twod &rhs)//ASSIgnment OPERAtor
             {
                        row = rhs.row;
                        col = rhs.col;
                        if (data)
                        delete[] data;
                        data = new int [(row * col)];
                        memcpy(data,rhs.data,sizeof(int) * row * col);
             }
             twod operator = (const twod &rhs)
             {
                            row = rhs.row;
                            col = rhs.col;
                            if (data)
                            delete[] data;
                            data = new int [row * col];
                            memcpy(data,rhs.data,sizeof(int) * row * col);
             }
             int& operator () (int r , int c)
             {
                  if (r < row && r > 0 && c < col && c > 0)
                  {
                        int temp = (*data) * r + c;//THATS the prob,.,.,.,.,
                        return temp;
                  }
             }
};
int main()
{
twod a , b(2,3);
/*for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
        b(i,j) = (0);
}
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
        cout << b(i, j) << endl;
}

system("pause");  
return 0;
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 4 Sep, 2007 - 10:20 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


That is C++ code. I asked you to post the actual errors you are receiving. An error message commonly takes the following form:
QUOTE

syntax error: missing ';' before <sometoken>

or items of that nature.

What are the error messages?
User is online!Profile CardPM

Go to the top of the page

freshman_prog
post 4 Sep, 2007 - 10:38 AM
Post #5


New D.I.C Head

*
Joined: 23 Mar, 2007
Posts: 23


My Contributions


QUOTE(Amadeus @ 4 Sep, 2007 - 11:20 AM) *

That is C++ code. I asked you to post the actual errors you are receiving. An error message commonly takes the following form:
QUOTE

syntax error: missing ';' before <sometoken>

or items of that nature.

What are the error messages?

ERRORS: In member function `int& twod::operator()(int, int)':
and whhen i remove /* from main then during compiling prog stop working 2dsafearray.exe stop working
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 4 Sep, 2007 - 10:45 AM
Post #6


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(freshman_prog @ 4 Sep, 2007 - 11:38 AM) *

QUOTE(Amadeus @ 4 Sep, 2007 - 11:20 AM) *

That is C++ code. I asked you to post the actual errors you are receiving. An error message commonly takes the following form:
QUOTE

syntax error: missing ';' before <sometoken>

or items of that nature.

What are the error messages?

ERRORS: In member function `int& twod::operator()(int, int)':
and whhen i remove /* from main then during compiling prog stop working 2dsafearray.exe stop working

That's because /* starts a comment block & */ ends the comment block
CODE

#include <stdio.h>

int main(void) {
  int i=0;

  /*
    This is a comment block.

    I can post useful information about the code.
  */

  for(i=0;i<100;i++) {
  /*
    printf("Or I can comment out code so that it will not execute\n");
  */
    printf("\n");

  return 0;
}


You define twod as follows:
CODE

twod(const int r ,const int c)

Yes you use it like this:
CODE

twod a , b(2,3);

I think the compiler is seeing twod with no arguments, a with no arguments, & then function b with arguments 2 & 3, end command. So twod & a have no semi-colon.

But I'm not the best with classes & could very well be wrong.
User is offlineProfile CardPM

Go to the top of the page

Bench
post 4 Sep, 2007 - 12:47 PM
Post #7


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


QUOTE(no2pencil @ 4 Sep, 2007 - 07:45 PM) *

You define twod as follows:
CODE

twod(const int r ,const int c)

Yes you use it like this:
CODE

twod a , b(2,3);

I think the compiler is seeing twod with no arguments, a with no arguments, & then function b with arguments 2 & 3, end command. So twod & a have no semi-colon.

But I'm not the best with classes & could very well be wrong.

There's nothing syntactically wrong with any of that - twod is a class, with 3 overloaded constructors-
twod(); (Default constructor)
twod(const int, const int);
twod(const twod&); (copy constructor)

This post has been edited by Bench: 4 Sep, 2007 - 01:00 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 4 Sep, 2007 - 12:50 PM
Post #8


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(Bench @ 4 Sep, 2007 - 01:47 PM) *

QUOTE(#2pencil is the coolest @ 4 Sep, 2007 - 12:00 PM) *

I think the compiler is seeing twod with no arguments, a with no arguments, & then function b with arguments 2 & 3, end command. So twod & a have no semi-colon.

But I'm not the best with classes & could very well be wrong.

There's nothing wrong with any of that - twod is a class, with 3 overloaded constructors-
twod(); (Default constructor)
twod(const int, const int);
twod(const twod&); (copy constructor)

Thank you. Sorry I was wrong =-)
User is offlineProfile CardPM

Go to the top of the page

Bench
post 4 Sep, 2007 - 01:01 PM
Post #9


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


For the OP - have a look what happens in the for-loop with the call to the overloaded () operator
CODE
             int& operator () (int r , int c)
             {
                  if (r < row && r > 0 && c < col && c > 0)
                  {
                        int temp = (*data) * r + c;//THATS the prob,.,.,.,.,
                        return temp;
                  }
             }

Returning a local function variable by-reference is almost guaranteed to crash.. the function ends, and 'temp' ceases to exist.

CODE
twod a , b(2,3);
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
        b(i,j) = (0)
}


b(i,j) = (0); This line is accessing a variable that has already been destroyed


From the overloaded () operator, I can only assume you probably want to return a reference to a position in your 2D array..?
perhaps you intended 'temp' to be a reference itself..?
CODE
int& temp = data[ col*r + c ];
return temp;


This post has been edited by Bench: 4 Sep, 2007 - 01:06 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:56AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month