invalid operands of types unsigned int* to binary operator%

This is a discussion on invalid operands of types unsigned int* to binary operator% within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to load a bitmap in. I have decided to create a class called ImageLoader, and a ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    invalid operands of types unsigned int* to binary operator%

    Hi, I am trying to load a bitmap in.
    I have decided to create a class called ImageLoader, and a class called Image.
    ImageLoader loads in all the information about the image, and then passes it to a temporary image. From main() i can then call the image and store it as its own image, while using the same ImageLoader to load in more images. (Some of you here helped with the code...)
    so one of the member variables of the loader is

    unsigned int * pixelData;

    if the file opens succesfully info (width, height, offset etc) are set to variables, and then the following method is called:
    Code:
    unsigned int * ImageLoader::readPixelData()
    {
        // Dynamically create a 2D array.
        pixelData = new unsigned [imageWidth*imageHeight];
    
        // Position file pointer to start of pixel data.
        file.seekg(offSet);
    
        // Read pixel data.
        for(int y = 0; y < imageHeight; y++)
        {
            char byte;
            for(int x = 0; x < imageWidth; x++)
            {
                unsigned n;
                file.get(byte);
                n = (byte & 0xff);
                file.get(byte);
                n = (n << 8) | (byte & 0xff);
                file.get(byte);
                n = (n << 8) | (byte & 0xff);
                pixelData[(y*imageWidth) + x] = n;
                //cout << setw(8) << pixelData[(y*imageWidth) + x] << '\n';
            }
    // Skip padding bytes at the end of each row.
            for (int i = 0; i < 4 - pixelData % 4; i++)
                file.get(byte);
    
            //cout << '\n';
        }
        file.close();
        return pixelData;
    }
    the only error my code has in it is on that for loop there:
    Code:
    for (int i = 0; i < 4 - pixelData % 4; i++)
    The error is:
    invalid operands of types unsigned int* to binary operator%
    Now this used to work fine, so Im not sure what I broke, but the error (and google) aren't being the greatest of helps here.
    Thanks

    BTW just checking: the method above is called like:
    Code:
    image.setPixelData(readPixelData());
    which is expecting:
    Code:
    void Image::setPixelData(unsigned int * data){
    I am calling delete [] pixelData;
    Is that right? - I have already passed the array to the image object that it belongs...

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    if pixelData is a 2D array as you said, when you write
    Code:
    pixelData%4
    what do you expect to get?
    I mean you use to get the modulus of numbers....
    If numbers are not integers use fmod (this is not your case, i am just saying that to you ).

    In code what i mean
    Code:
    int array[5][5]; //2D array of ints
    
    for(int i = 0 ; i < 5 ; i++) {
       for(int j = 0 ; j < 5 ; j++) {
             array[i][j] = 5;
       }
    }
    int m = array[3][3] % 2;
    ...

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    I can see what you want to do, but what you have is very wrong. You intend pixelData % 4 to be the number of bytes to skip for the padding at the end of each line. However even if pixelData were an integer, pixelData % 4 would still always be zero because the array is guaranteed to be aligned on a 4-byte boundary.
    What line 26 should be is:
    Code:
    for (int i = 0; i < (4 - imageWidth % 4) % 4; i++)
    And yes, you must mod with 4 twice, we covered this recently in another thread.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Hi guys, thanks for you help.
    I have got this compiling now, and quite happy however I dont quite trust the output.
    I would appreciate it if someone could go here:
    https://bitbucket.org/amlwwalker/bit...ion/changesets

    I havewritten comments on the things I am not sure i have done correctly, if anyone could have a look and comment back here (or there if you have a bitbucket account) that would be very helpful, although if there is one thing you look at please could you look at the output file (box1bmp_output.txt) The image, box1.bmp is just a white box, and the values come out pretty close to that, however right at the end, I get a load of zero's - which is black. There is no black in the image...

    Thanks

    Alex

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Yeah, its picking up the attached image as:
    7137336, 4067752, 16777215, 16777215, 0, 0, 16777215, ||
    0, 0, 255, 16777215, 16776960, 0, 255, ||
    16711680, 0, 0, 65535, 16777215, 16711680, 0, ||
    16777215, 0, 0, 0, 16777215, 16777215, 0, ||
    255, 16776960, 0, 0, 255, 16777215, 16776960, ||
    The || denotes the end of a line.
    What are those random numbers - the image is only black and white...

    This is my code reading in the pixel data:
    Code:
    void ImageLoader::readPixelData()
    {
        // Dynamically create a 2D array.
        pixelData = new unsigned int [imageWidth*imageHeight];
        cout << "width: " << imageWidth << " height: " << imageHeight << " " << sizeof(pixelData) << endl;
        // Position file pointer to start of pixel data.
        file.seekg(offSet);
    
        // Read pixel data.
        for(int y = 0; y < imageHeight; ++y)
        {
            char byte;
            for(int x = 0; x < imageWidth; ++x)
            {
                unsigned n;
                file.get(byte);
                n = (byte & 0xff);
                file.get(byte);
                n = (n << 8) | (byte & 0xff);
                file.get(byte);
                n = (n << 8) | (byte & 0xff);
                pixelData[(imageWidth*y) + x] = n;
                //cout << setw(8) << imageData[(imageWidth*y) + x] << '\n';
            }
    // Skip padding bytes at the end of each row.
            for (int i = 0; i < (4 - imageWidth % 4) %4 ; i++)
                file.get(byte);
        }
        image.setPixelData(pixelData);
        delete [] pixelData; //deletes pixelData
    }
    But theres something fishy going on, Im not sure its cutting the padding off correctly? Any ideas?
    Attached Images Attached Images  
    Last edited by a.mlw.walker; 11-15-2012 at 01:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting error: invalid operands to binary &
    By P6nn in forum C Programming
    Replies: 3
    Last Post: 05-06-2012, 04:27 AM
  2. Replies: 2
    Last Post: 12-26-2009, 09:07 AM
  3. Invalid operands to binary *
    By jrfall349 in forum C Programming
    Replies: 8
    Last Post: 03-31-2007, 11:15 PM
  4. invalid operands to binary %
    By Mathsniper in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 10:21 PM
  5. invalid operands to binary ^ ?
    By seal in forum C Programming
    Replies: 14
    Last Post: 09-13-2005, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21