|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| olove05 (60) | |
| somebory can tell me how to swap two colum of the two dimentional array that with pointer | |
|
|
|
| Duoas (5977) | |
|
Use a loop. It should iterate over the rows. For each row, swap the two column values. | |
|
|
|
| olove05 (60) | |
|
I did but do not work I was in function swap 0 0x11 0 sh: pause: command not found I was in destructor *** glibc detected *** ./a.out: free(): invalid pointer: 0x0991d070 *** ======= Backtrace: ========= /lib/libc.so.6[0x5d5a68] /lib/libc.so.6(__libc_free+0x78)[0x5d8f6f] /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xade801] /usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xade85d] ./a.out[0x8048e1d] ./a.out[0x8048efd] /lib/libc.so.6(__libc_start_main+0xdc)[0x5874e4] ./a.out(__gxx_personality_v0+0x51)[0x8048791] ======= Memory map: ======== 00555000-0056e000 r-xp 00000000 fd:00 1403592 /lib/ld-2.4.so 0056e000-0056f000 r-xp 00018000 fd:00 1403592 /lib/ld-2.4.so 0056f000-00570000 rwxp 00019000 fd:00 1403592 /lib/ld-2.4.so 00572000-0069f000 r-xp 00000000 fd:00 1403660 /lib/libc-2.4.so 0069f000-006a1000 r-xp 0012d000 fd:00 1403660 /lib/libc-2.4.so 006a1000-006a2000 rwxp 0012f000 fd:00 1403660 /lib/libc-2.4.so | |
|
Last edited on
|
|
| Zhuge (2205) | |
| Looks like you are free()ing an invalid pointer. We would probably need to see code. | |
|
|
|
| olove05 (60) | |
|
#include <iostream> #include <string> using namespace std; /*TwoDimArray::TwoDimArray() { } */ TwoD::TwoD( int r, int c) { x = r; y = c; elemtD = new int *[x]; // **elemtD is a two Dim pointer in the private sector for (int i = 0; i < x; i++){ elemtD[i] = new int [y]; } for (int i=0; i < x; i++){ for(int j=0; j < y; j++){ elemtD[i][j] = i*j; } } } TwoD::TwoD(const TwoD & ap1) // copy constructor { deepCopy( ap1 ); } TwoD::~TwoD( ) { for (int i = 0; i < x; i++) { delete[] elemtD[i]; delete[] elemtD; } } /*void TwoD::operator =( const TwoDimArray &right1 ) // overload assigment operator give run time error { if (this == &right1 ) return this; delete elemtD; deeCopy (right1); return *this; } TwoD::~TwoD( ) { for (int i = 0; i < x; i++) { delete[] elemtD[i]; delete[] elemtD; } } /*void TwoD::operator =( const TwoD &right1 ) // overload assigment operator give run time error { if (this == &right1 ) return this; delete elemtD; deeCopy (right1); return *this; } */ /*void TwoD:: operator[](int i, int j);// give run time erros { return elemtwoDarray[i][j]; } */ void TwoD::deepCopy(const TwoD & orig1 ) { x = orig1.x; y = orig1.y; elemtD= new int *[x]; // dynamically allocated array of int of set rows length for (int i = 0; i < x; i++) { elemtD[i] = new int[x]; // Fill up row until x row } for ( int i = 0; i < x; i++ ) { for ( int j = 0; j < y; j++ ) { elemtD[i][j] = orig1.elemtD[i][j]; //elemtD[j] = orig2.elemtD[j]; } } } int TwoD::print( int r , int c) { x= r y=c; for (int i = 0; i < x; i++) { for(int j=0; j < y; j++) { cout<<" " << elemtD[i][j] <<" "; } putchar('\n'); cout<<endl; } } int TwoD::swap(int index1, int index2 ) { x = index1; y = index2; int *temp; for (int i = 0; i< x; i++) { temp = elemtD[y]; elemtD[ydim] = elemtD[x]; elemtD[x] = temp; cout<<" " << elemtD[x]<<" "; } // putchar('\n'); cout<<endl; } ------------------------------------------------------------------- in main function #include <iostream> #include <string> #include "numero3.h" using namespace std; int main() { TwoD TwD(3,4); // Declaring the three TwoD objects containing cout << "\nElement values in the 2D Array are:\n"; TwD.print( 3,4 ); // Creating the arrays TwD.deepCopy(TwD);// I have a question. If I have this function in the private section how I can call it in main? cout << "\n The Swap element values in the 2D Array are:\n"; TwD.swap( 3,4 ); system("pause"); return 0; }; | |
|
|
|
| olove05 (60) | |
| Hello, no answer to this topic or any clue | |
|
|
|
| guestgulkan (2375) | |||
|
Well to get you started: 1. The assignment operator return type should be a reference NOT void. 2. In the assignment operator function - your delete code should be the same way it is done in the destructor.
| |||
|
|
|||
| olove05 (60) | |
| thanks a lot | |
|
|
|
| guestgulkan (2375) | |||
While I'm thinking about it - the actual delete in the destructor is not quite right:
There appear to be another big issue in that some functions that would not be expected to change the object (for example the print function) actually does change the object ( the x andy y values). That can't be good. | |||
|
Last edited on
|
|||
| olove05 (60) | |
|
why my function just change the rows I want to change the columns 0 0 0 0 | 0 2 3 4 0 1 2 2 | 0 1 2 2 0 2 3 4 | 0 0 0 0 after swap we have (0,2) wants 0 0 0 0 2 1 2 0 4 2 3 0 some clue that I can use to have the result that I want? | |
|
Last edited on
|
|
| olove05 (60) | |
| any clue guys? | |
|
|
|