I'm working on my first game that isn't a test, in visual C++ using DarkGDK's library. I have never used pointers, and I need help debugging a snippet. I have map data, stored in an array called "level1". It's 10 by 10. What I want it to do is filter through, find the amount of bricks and then display them in their correct locations... but It's giving me errors with the pointers...
void createLevelBasedOnData(){ /Obviously the function I call to create the level.
C3DObjectCube *pCube = new C3DObjectCube[countBricks()]; // My dad says this will allocate enough memory for countBricks amount of objects. And to step through it should be easy.
for(int x = 0; x < 10; x++){ //Filter through all the 10 by 10 bricks.
for(int y = 0; y < 10; y++){ //Look here^^
if(level1[y][x] == 1){ //If there is a 1 there, draw a tile.
*pCube[y*x]->ID = y*x; //setting the individual ID of the brick...
*pCube[y*x]->setPosition((x*10)+5,5,(y*10)+5); //Setting it to fit the tileboard.
*pCube[y*x]->size = 10; //Making it fit the tileboard.
*pCube[y*x]->create(); //Create the object.
*pCube[y*x]->upDate(); //UpDate it's position...
}
}
}
return;
}
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
1>c:\documents and settings\billy\my documents\visual studio 2008\projects\yetanotherfailedtest\yetanotherfailedtest\main.cpp(90) : error C2440: 'initializing' : cannot convert from 'C3DObjectCube *' to 'C3DObjectCube []'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>c:\documents and settings\billy\my documents\visual studio 2008\projects\yetanotherfailedtest\yetanotherfailedtest\main.cpp(95) : error C2232: '->C3DObjectCube::ID' : left operand has 'class' type, use '.'
1>c:\documents and settings\billy\my documents\visual studio 2008\projects\yetanotherfailedtest\yetanotherfailedtest\main.cpp(96) : error C2819: type 'C3DObjectCube' does not have an overloaded member 'operator ->'
1> c:\documents and settings\billy\my documents\visual studio 2008\projects\yetanotherfailedtest\yetanotherfailedtest\main.cpp(13) : see declaration of 'C3DObjectCube'
You should still use "C3DObjectCube *pCube = new C3DObjectCube[countBricks()];".
When you do "C3DObjectCube *pCube", you're declaring a C3DObjectCube pointer.
When you use pCube, you won't use the * anymore. To make it easier to read, I would put the * on the side of the type, like this:
C3DObjectCube* pCube = new C3DObjectCube[countBricks()];
-Walter
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
void createLevelBasedOnData(){
int brickNum = 0;
C3DObjectCube* pCube = new C3DObjectCube[countBricks()];
for(int x = 0; x < 10; x++){
for(int y = 0; y < 10; y++){
if(level1[y][x] == 1){
pCube[brickNum].ID = y*x;
pCube[brickNum].setPosition((x*10)+5,5,(y*10)+5);
pCube[brickNum].size = 10;
pCube[brickNum].create();
pCube[brickNum].upDate();
brickNum++;
}
}
}
return;
}
pCube is a pointer, and when you do pCube[someindex], it's the same as doing *(pCube+someindex). The value at pCube+someindex is a C3DObjectCube object (or struct), not a pointer. So to access it's members, you would use the . (dot) operator.
-Walter
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
C3DObjectCube* pCube = new C3DObjectCube[coutnBricks()];
You are making a pointer, then allocating memory using "new" the size of countBricks(). C3DObjectCube* pCube points to the array you just allocated and you can access it just like a normal array. Also when you are using "new", make sure to always do
string someString = "part 1 " + "Part 2"; // This will fail
As long as one of the objects is a string object (std::string), you can chain together strings and char arrays.
This is because the + operator is just an overloaded function that takes std::string and a char array, or two std::string's.
// This:
string str1 = "text";
string str2 = str1 + "other text" + "some more text";
// Is equivalent to this:
string str1 = "text";
string str2 = operator+(operator+(str1, "other text"), "some more text");
-Walter
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
GyroVorbis wrote:I moved this to the programming discussion, as this forum is reserved for general project announcements/updates. Kthx. :D
I'm sorry, I just had to quote this, is this Falco ACTUALLY MODERATING THE FORUMS!?
On topic: I know nothing about Dark GDK, so I can't really help. One thing I would say though, is it really necessary to comment every single line? I mean, it's obvious what "pCube[brickNum]->create();" and "pCube[brickNum]->upDate();" do. Still, better than not leaving any comments and having no clue what you were doing weeks later, I suppose.
¡SÃ! ¡He dejado en libertad los prisioneros y ahora vengo por ti!
~El Pollo Diablo
ibly31 wrote:Oh, i did that for people to be able to help me the most they can...
Well since the icons on the main section of the forums turn read when you haven't read a post (and ALL new posts show up in "posts since last visit" at least once) it really doesn't matter where you post it, it's just as likely to be read. No big deal, now you know.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!