开发者

Problems trying to assign **varname to *varname[]. c++

In my .h, I have a variable, Texture ** skyboxTextures. I assign some texture pointers in one method, and use them right away:

Texture *skt[] = { 
    tleft,
    tright,
    tfront,
    tback,
    tup,
    tdown
};
skyboxTextures = skt;

for(int i = 0; i < 6; i++)
{
    skyboxTextures[i]->load();
}

Then later in another method I try to use the textures again.

Texture *skt[] = skyboxTextures;

// Render the front quad
skyboxTextures[0]->activate();

This is my issue I cannot access my objects any more. This will not compile because of this error:

error C2440: 'initializing' : cannot convert开发者_StackOverflow社区 from 'Texture **' to 'Texture *[]'

If I comment out the line Texture *skt[] = skyboxTextures;, all I get are invalid texture pointers.


You cannot assign arrays. Unfortunately, it's difficult to know exactly what to suggest, as you've only provided a small snippet of your code. I would recommend reading this, however: http://c-faq.com/aryptr/index.html.


Then later in another method I try to use the textures again.

You cannot do that. Converting an array into a pointer to it's first element is a one way process. While the array is still an array the compiler has information like the size of the array available at compile time. Once it's a pointer, the compiler no longer has that information.

You either have to reference skt itself, or just not convert skyboxTextures back to an array at all. skyboxTextures[0]->activate(); should work just as you would expect.

If I comment out the line Texture *skt[] = skyboxTextures;, all I get are invalid texture pointers.

My guess is that the issue lies here:

Texture *skt[] = { 
    tleft,
    tright,
    tfront,
    tback,
    tup,
    tdown
};

You must ensure tleft, tright, tfront, tback, tup, and tdown are valid when you use the other array. If you did something like allocated them on the stack and then returned skyboxTextures as a pointer from your method then you'd have undefined behavior. You'll probably have to put these on a heap allocation.


Teal,

Try updating this line Texture *skt[] = skyboxTextures;

to Texture **skt = skyboxTextures;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜