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!
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; }
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; }
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 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.
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
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)
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