News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Compile error

Started by sethjackson, February 05, 2006, 10:22:34 PM

Previous topic - Next topic

sethjackson

Hi I get this error

error: incompatible types in assignment of `int' to `unsigned char*[((unsigned int)((int)imageSize))]'

with this code......

Code (cpp) Select

if (image.HasAlpha())
{
        unsigned long imageSize = image.GetWidth() * image.GetHeight() * 4;
        unsigned char* imageData = image.GetData();
        unsigned char* imageAlpha = image.GetAlpha();
        unsigned char* data[imageSize];

        for (unsigned long index = 0; index < imageSize; index += 4)
        {
            memcpy(data, imageData, 3);

            imageData += 3;

            memcpy(data + 3, imageAlpha, 1);

            imageAlpha++;

            data += 4;
        }

        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(),
                          GL_RGB, GL_UNSIGNED_BYTE, data);
}



Help me please...... Sorry for the beginner question? I just don't get it I used to do stuff like increaseing the ptr with += all the time.....

EDIT:

Let me say that it fails on the line

Code (cpp) Select

data += 4;


:P

sethjackson

Ok I fixed it now could someone tell me why this works???

Code (cpp) Select

*(data) += 4;

polygon7

#2
Hi,
this should work
if (image.HasAlpha())
{
        unsigned long imageSize = (image.GetWidth() * image.GetHeight())  << 2;
        unsigned char* imageData = image.GetData();
        unsigned char* imageAlpha = image.GetAlpha();
        unsigned char* data[imageSize];

        for (unsigned long index = 0; index < imageSize; index += 4)
        {
            memcpy(data, imageData, 3);

            imageData += 3;

            memcpy(data + 3, imageAlpha, 1);

            ++imageAlpha;

           (*data) += 4;
        }

        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(),
                          GL_RGB, GL_UNSIGNED_BYTE, data);
}

// Edit: Ah, you're faster :P
char* array[size] is the same as char** array
best regards,
p7
Free open source UML modeling tool: ArgoUML

sethjackson

 :lol: :lol: Read my post above. Exactly why does it work though? Thanks.  :D

Michael

Quote from: sethjackson on February 05, 2006, 10:40:22 PM
:lol: :lol: Read my post above. Exactly why does it work though? Thanks.  :D

You tried to assign an int to an unsigned char*. This does not work for sure. What do did is to de-reference the pointer. Then it works.

Why you cast before int and then unsigned int?

Anyway, do not use C-cast style. This is not a good idea in C++. You should use C++ cast style or better no cast at all (if possible).

Best wishes,
Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

sethjackson

@polygon7

Why the
Code (cpp) Select
<< 2 instead of
Code (cpp) Select
* 4?

This line

Code (cpp) Select

unsigned long imageSize = (image.GetWidth() * image.GetHeight())  << 2;


@Michael How to use a C++ style cast in this case??? Yes I know static_cast, dynamic_cast etc. How to use it here???

polygon7

#6
Quote from: sethjackson on February 05, 2006, 10:55:55 PM
@polygon7

Why the
Code (cpp) Select
<< 2 instead of
Code (cpp) Select
* 4?

This line

Code (cpp) Select

unsigned long imageSize = (image.GetWidth() * image.GetHeight())  << 2;

"<<" is shifting bits operator (shift left, SHL in assembly). SHL can be used for multiply by power of two (SHR, shift right for dividing by power of two).

x*2 == x <<1, x*4 == x <<2 ...

Bit shifting usually is faster then normal multiply.
best regards,
p7
Free open source UML modeling tool: ArgoUML

Michael

Quote from: sethjackson on February 05, 2006, 10:55:55 PM
@Michael How to use a C++ style cast in this case??? Yes I know static_cast, dynamic_cast etc. How to use it here???

In your case and if I am not wrong, I would use the static_cast Operator:

Quote
static_cast<T> (expr)
To convert the expression expr to type T. Such conversions rely on static (compile-time) type information.

Anyway, I am not an expert in C++ cast as usually I used C cast until I discovered that are not so good (cast is already ugly, but C-cast is uglier). You can get some useful info here:


You can also have a look at this topic.

Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

sethjackson

Ok guys thanks. I learnt something today. :)

@Michael yeah I always use C++ casts instead of C casts. I couldn't figure out how to get it to work here. What type do I need to static_cast to?? I tried static_cast, but I couldn't figure out which type to cast to.  :P

Michael

Quote from: sethjackson on February 06, 2006, 12:01:07 AM
@Michael yeah I always use C++ casts instead of C casts. I couldn't figure out how to get it to work here. What type do I need to static_cast to?? I tried static_cast, but I couldn't figure out which type to cast to.  :P

For example, you can use it to cast a double to an int:


double myDouble = 3.0;
int myInt = static_cast<int>(myDouble);


Anyway, be careful because static casts could be dangerous.

Have a look at the links I have suggested. There is a lot of examples and useful bla bla :).

Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

sethjackson

Ok I'll check it out tomorrow hopefully.  :lol:

sethjackson

#11
I tried

Code (cpp) Select

static_cast<unsigned char* []>(data) += 4;


and it didn't work.


error: invalid static_cast from type `unsigned char*[((unsigned int)((int)imageSize))]' to type `unsigned char*[]'


What type (int, char, etc.) do I need to cast to???

sethjackson

Nevermind. This works, and better too I think. :)

Code (cpp) Select

if (image.HasAlpha())
{
        unsigned long imageSize = image.GetWidth() * image.GetHeight() * 4;
        unsigned char* imageData = image.GetData();
        unsigned char* imageAlpha = image.GetAlpha();
        unsigned char* data = new unsigned char[imageSize];

        for (unsigned long index = 0; index < imageSize; index += 4)
        {
            memcpy(data, imageData, 3);

            imageData += 3;

            memcpy(data + 3, imageAlpha, 1);

            ++imageAlpha;

            data += 4;
        }

        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(),
                          GL_RGB, GL_UNSIGNED_BYTE, data);

        delete[] data;
}

Michael

Hello,

When you use new you should check if the memory has been reserved or not. If I remember correctly the new sends a bad_allocation exception if it fails. Another version of new just a null pointer (which could or not be a better alternative :)).

Best wishes,
Michael
[url="http://img207.imageshack.us/img207/9728/411948picture4em.png"]http://img207.imageshack.us/img207/9728/411948picture4em.png[/url]

killerbot

Quote from: Michael on February 07, 2006, 10:42:10 AM
Hello,

When you use new you should check if the memory has been reserved or not. If I remember correctly the new sends a bad_allocation exception if it fails. Another version of new just a null pointer (which could or not be a better alternative :)).

Best wishes,
Michael


Normally when new fails, it throws -> catch it.
There exist a no_throw new. I think it is not portable, the gurus (Sutter, ..) give the advice not to use it. So just forget you ever heard about it ;-)