1. #1

    question about accessors/mutators in C++

    I am trying to call my class mutator function with "objectName.setItem(getItem, value)" but for some reason the data doesn't get changed. What are the most common causes for this? I can include snippets of code if this is too vague.
    I'm sorry, but i just have to tell you that you are an ignorant, closed minded fool, and you need to chill out, think more, and quit being such a douche. Your responses are shrill, closed minded, and ignorant. You need to think outside the box sometimes, and realize its blizzards game and you are not ghostcrawler.

  2. #2
    Its been a while since I did C++, but yes its too vague at least for me.
    Yesterday is history, today is a gift, tomorrow is mystery.

  3. #3
    Ok here is the calling function:

    void convertImage(MyImage &convertThis) {

    pixel thisPixel;
    int redColor = 0;
    int greenColor = 0;

    for (int row = 0; row < convertThis.getRows(); row++) {
    for (int col = 0; col < convertThis.getCols(); col++) {

    thisPixel = convertThis.getPixel(row, col);

    redColor = thisPixel.red + (row % 7);
    convertThis.setPixel(thisPixel, "red", redColor);

    greenColor = thisPixel.green - (col % 11);
    convertThis.setPixel(thisPixel, "green", greenColor);
    }
    }
    }

    And here is the class mutator function:

    void MyImage::setPixel(pixel &p, string color, int val) {

    if (val > 255) val = 255;
    if (val < 0) val = 0;
    if (color == "red") p.red = val;
    else if (color == "blue") p.blue = val;
    else p.green = val;
    }
    I'm sorry, but i just have to tell you that you are an ignorant, closed minded fool, and you need to chill out, think more, and quit being such a douche. Your responses are shrill, closed minded, and ignorant. You need to think outside the box sometimes, and realize its blizzards game and you are not ghostcrawler.

  4. #4
    Its been a while so I apologise your logic looks sound enough, but if it isnt changing then it isn't being mutated obviously... I dont have C++ on my pc or id have a little poke around, id suggest stepping through your program and putting watches on your variables.

    Nothing stands out to me as being wrong, but its been like 7 years since I did this so I am somewhat rusty
    Yesterday is history, today is a gift, tomorrow is mystery.

  5. #5
    It doesn't look like the error is in the code you posted.
    Best way to find the problem would be a quick debugger runthrough.
    If the radiance of a thousand suns were to burst at once into the sky, that would be like the splendor of the Mighty One... now I am become Death, the Destroyer of Worlds.

  6. #6
    Quote Originally Posted by starvethedead View Post
    pixel thisPixel;
    This appears to be your problem. You are declaring a local variable then changing it with the mutator. You need to use pointers to get it to work the way you want.

  7. #7
    Quote Originally Posted by ascii View Post
    This appears to be your problem. You are declaring a local variable then changing it with the mutator. You need to use pointers to get it to work the way you want.
    oh he's right, idk how I missed that. You don't have to use pointers though... and you probably shouldn't as it defeats the purpose of having a mutator. You could do it like this:



    void convertImage(MyImage &convertThis) {

    pixel thisPixel;
    int redColor = 0;
    int greenColor = 0;

    for (int row = 0; row < convertThis.getRows(); row++) {
    for (int col = 0; col < convertThis.getCols(); col++) {

    thisPixel = convertThis.getPixel(row, col);

    redColor = thisPixel.red + (row % 7);
    convertThis.setPixel(row,col, "red", redColor);

    greenColor = thisPixel.green - (col % 11);
    convertThis.setPixel(row,col, "green", greenColor);
    }
    }
    }

    And here is the class mutator function:

    void MyImage::setPixel(int row, int col, string color, int val) {

    if (val > 255) val = 255;
    if (val < 0) val = 0;
    if (color == "red") p.red = val;
    else if (color == "blue") p.blue = val;
    else p.green = val;
    //replace those 3 things with the actual MyImage pixel, using row and col.
    }
    If the radiance of a thousand suns were to burst at once into the sky, that would be like the splendor of the Mighty One... now I am become Death, the Destroyer of Worlds.

  8. #8
    taekvideo's code looks good. Generally a mutator takes parameters and sets variables in the class, what you had was returning the variable by reference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •