Passing 2D array elements to another class

closed account (jiN8b7Xj)
Hello
http://i.stack.imgur.com/bzeuD.png this is a similar UMl Digram to what I have,, in FourthClass (EditorClass) I have a Function that takes the values of Arr** (pixels). and change them. I Make an object of FourthClass(Editor) that inherit from The third that has a data member of SecondClass (Image);

this is my code were I'm trying to use the 2d array I just made to rotate the 2d array 90

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 void Editor::newProject(){// From here 
    Num=1;
    cout<<"Name Of The Image: ";
    cin>>name;
    trans();}
    
void Editor::trans(){
    I=new Image[Num];
    I[0].setID(name);
    I[0].load(name); //Until here everything okay

//Now I want to call the values of pixels to do the swapping 

//*****The basic rotating function******* //Should be edited
void Editor::rotate(int d){
    switch (d){
        case 90: for(int i=0; i<W; i++){
            for(int j=i; j<H; j++){
                swap(pixels[i][j], pixels[j][i]);}}
            //reverse elements on row order
            for(int i=0; i<W; i++){
                for(int j=0; j<H/2; j++){
                    swap(pixels[i][j], pixels[i][H-j-1]);}
.
.
.
} 

Since pixels** is a private data member of the ImageClass (SecondClass) I need to use getpixel function

1
2
RGB Image::getPixel(int i,int j){
    return pixels[i][j];} //Pixels are a 2D array Type class RGB 


Here I tried to use rotate function calling getPixel
1
2
3
4
5
6
 for (int r=0; r<(I[0].getW()); r++) {
        for (int c=0  ; c<(I[0].getH()); c++) {
            swap(I[0].getPixel(r, c), I[0].getPixel(r, c))}
.
.
.


I used getPixel so I could call each element one by one to do the swapping but an error occur says "No matching swap function"
I don't know why that happen and how to fix it

what do you think?
Thank you
Eric
Last edited on
It would appear the getPixel class returns a copy of the RGB instance in question, so swapping them would have no effect.

Your compiler is barfing because the unnamed variable returned from getPixel is a temporary and you're trying to bind that temporary to a non-const reference (as it wouldn't make sense for swap to take values by const reference) which can't be done.
closed account (jiN8b7Xj)
Ah that's right , so what should I use to fix this to do the swapping with the real values?
closed account (jiN8b7Xj)
just to test my code I make class Image data member public the and I wrote my rotate function like this :
1
2
3
4
5
6
7
8
9
void Editor::rotate(int d){
    switch (d){
        case 270: for(int i=0; i<I[0].W; i++){
            for(int j=i; j<I[0].H; j++){
                swap(I[0].pixels[i][j], I[0].pixels[j][i]);}}
            //reverse elements on row order
            for(int i=0; i<I[0].W; i++){
                for(int j=0; j<I[0].H/2; j++){
                    swap(I[0].pixels[i][j], I[0].pixels[i][I[0].H-j-1]);}}


but there was no change in the array values (seeing the image I have)..
:( what to do?
but there was no change in the array values (seeing the image I have)..

It should flip the image around the horizontal axis. Of course, that's all in the second loop. The first loop has no effect since it works beyond the halfway point and undoes in the second half what it did in the first.

Rotating 90 degrees (or 270 degrees) isn't a symmetric operation. I don't think swap is going to be the answer for you.
Last edited on
Topic archived. No new replies allowed.