Page 1 of 2
Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 4:02 pm
by ibly31
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...
Code: Select all
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;
}
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 4:11 pm
by wtetzner
The "C3DObjectCube *" in "C3DObjectCube *pCube" is actually the data type. So when you access memory at that location, use pCube instead of *pCube.
For example, where you have
you should have
-Walter
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 4:23 pm
by ibly31
Hmm. Didn't work. Now I have this:
Code: Select all
void createLevelBasedOnData(){
int brickNum = 1;
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;
}
and I get this:
Code: Select all
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
and this:
Code: Select all
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'
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 4:38 pm
by wtetzner
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:
Code: Select all
C3DObjectCube* pCube = new C3DObjectCube[countBricks()];
So C3DObjectCube* is the type of pCube.
Change
Code: Select all
C3DObjectCube pCube[] = new C3DObjectCube[countBricks()];
to
Code: Select all
C3DObjectCube* pCube = new C3DObjectCube[countBricks()];
-Walter
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 4:49 pm
by ibly31
I have this:
Code: Select all
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;
}
But it is saying that left of the -> operand is a class, so use "."...
Shouldn't it be:
Code: Select all
void createLevelBasedOnData(){
int brickNum = 0;
C3DObjectCube* pCube;
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;
}
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 4:59 pm
by wtetzner
That works.
You could also do this:
Code: Select all
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
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 5:02 pm
by Joeyotrevor
No, it should just be
When you are declaring
Code: Select all
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
when you are done with the array(end of program) or you will have a memory leak.
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 8:22 pm
by ibly31
Yeah, i got it to work now. Thanks guys! Anyone know how to set a Icon for a program in Microsoft Visual Studio C++?
EDIT: Also: sorry, how do you do... whats the term, um... string linking?
like in lua, to display "Character X: variable" I would do print("Character X"..charXVar"). How do you do this in C++?
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 8:29 pm
by MarauderIIC
Something about add resource or something
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 9:31 pm
by Joeyotrevor
ibly31 wrote:
like in lua, to display "Character X: variable" I would do print("Character X"..charXVar"). How do you do this in C++?
If you are using std::string you can do
Code: Select all
std::stringstream ss;
std::string str;
ss << "Character X:" <<charXVar;
ss >> str;
I think there is probably an easier way though.
Or if you are using char*:
Code: Select all
char* str;
sprintf(str, "Character X: %i", charXVar); // "%i" is replaced by the value of charXVar
Apparently sprintf is "unsafe" and you should use sprintf_s instead
Re: Need Help! Short Snippet, 15 lines.
Posted: Mon Feb 23, 2009 11:17 pm
by wtetzner
If you have two std::string objects, or if you have one std::string object and a char array, you can use the + operator.
Code: Select all
#include <string>
using namespace std;
void main()
{
string strOne = "some string";
string strFinal = strOne + " " + "some other string";
}
You can't use the + operator on two char arrays though.
Code: Select all
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.
Code: Select all
// 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
Re: Need Help! Short Snippet, 15 lines.
Posted: Tue Feb 24, 2009 6:45 am
by Falco Girgis
I moved this to the programming discussion, as this forum is reserved for general project announcements/updates. Kthx. :D
Re: Need Help! Short Snippet, 15 lines.
Posted: Tue Feb 24, 2009 10:32 am
by Chaos Clown
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.
Re: Need Help! Short Snippet, 15 lines.
Posted: Tue Feb 24, 2009 4:56 pm
by ibly31
Oh, i did that for people to be able to help me the most they can...
Re: Need Help! Short Snippet, 15 lines.
Posted: Tue Feb 24, 2009 5:42 pm
by dandymcgee
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.