Need Help! Short Snippet, 15 lines.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Need Help! Short Snippet, 15 lines.

Post 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;
}
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Need Help! Short Snippet, 15 lines.

Post 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

Code: Select all

*pCube[y*x]->ID = y*x;
you should have

Code: Select all

pCube[y*x]->ID = y*x;
-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.
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Need Help! Short Snippet, 15 lines.

Post 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'
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Need Help! Short Snippet, 15 lines.

Post 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
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.
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Need Help! Short Snippet, 15 lines.

Post 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;
}
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Need Help! Short Snippet, 15 lines.

Post 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
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.
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: Need Help! Short Snippet, 15 lines.

Post by Joeyotrevor »

No, it should just be

Code: Select all

pCube[brickNum].id
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

Code: Select all

delete [] pCube;
when you are done with the array(end of program) or you will have a memory leak.

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Need Help! Short Snippet, 15 lines.

Post 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++?
Last edited by ibly31 on Mon Feb 23, 2009 8:30 pm, edited 1 time in total.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Need Help! Short Snippet, 15 lines.

Post by MarauderIIC »

Something about add resource or something
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: Need Help! Short Snippet, 15 lines.

Post 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

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Need Help! Short Snippet, 15 lines.

Post 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
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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Need Help! Short Snippet, 15 lines.

Post by Falco Girgis »

I moved this to the programming discussion, as this forum is reserved for general project announcements/updates. Kthx. :D
Chaos Clown
ES Beta Backer
ES Beta Backer
Posts: 61
Joined: Sun Sep 21, 2008 4:46 am
Location: U.K.

Re: Need Help! Short Snippet, 15 lines.

Post 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!? :shock:

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
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: Need Help! Short Snippet, 15 lines.

Post by ibly31 »

Oh, i did that for people to be able to help me the most they can...
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Need Help! Short Snippet, 15 lines.

Post 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. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply