C++

Discussions about Coding and Scripting
Post Reply
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

C++

Post by Rakiayn »

hello,

A while ago I made the decision to stop developing my MOD unrealspawn. however I considered the coding to be a nice hobby.
So I decided to pick up C++ and make some 2d game/simulation.
I started a project and got some problems, hopefully someone can help me.
I am working a simulation/game that consists out of a 2d dimensional grid. the grid is filled with objects that make up the elements for the game.

so far I created two classes
the world class:
the world class can be seen as the 2d sheet with grids. at start of the program one world object must be created.
this object will contain information like what objects are on the grid

the groundtile class:
the groundtile class is like a square that is placed on the grid of the world


below is my code, not all of it but the code that is relevant for now

Code: Select all


//world class is the 2d map that contains all the tiles and objects
class world
{
	//function and variables under public can be called outside of the class
	public:

	//size of screen
	int screensize;
	//amount of squares on an axes in the screen
	int amountofgrids;
	int gridsize;
	int gridposition[20];

	void calculategridsize()
	{
		screensize=500;
		amountofgrids=100;
		gridsize = screensize/amountofgrids;
	}



	void createbackgroundtiles()
	{
	// creates a new tile
		
		groundtile* bravenewgroundtile = new groundtile();
		bravenewgroundtile->colorblue=0;
		bravenewgroundtile->colorgreen=0;
		bravenewgroundtile->colorred=0;
		bravenewgroundtile->varredmin=255;
		bravenewgroundtile->varredmax=255;
		bravenewgroundtile->vargreenmin=255;
		bravenewgroundtile->vargreenmax=255;
		bravenewgroundtile->varbluemin=255;
		bravenewgroundtile->varbluemax=255;
		bravenewgroundtile->createself();
	}

	//only functions inside this class can acces the functions and variables under private
	private:
};

// creates a new world
world* bravenewworld = new world();

//bravenewworld->calculategridsize();
//bravenewworld->createbackgroundtiles();

//groundtile class are the background tiles that are on the grid of the world
class groundtile
{
	//function and variables under public can be called outside of the class
	public:	

	//position variables
	int tileposx,tileposy;

	//color variables
	int colorred, colorgreen, colorblue;
	int varredmin,vargreenmin,varbluemin;
	int varredmax,vargreenmax,varbluemax;

	void createself()
	{
		totalred = decidecolor(varredmin,colorred,varredmax);
		totalgreen = decidecolor(vargreenmin,colorgreen,vargreenmax);
		totalblue = decidecolor(varbluemin,colorblue,varbluemax);
		drawsquare(bravenewworld->gridsize,bravenewworld->gridsize,totalred,totalgreen,totalblue);
	}

	//only functions inside this class can acces the functions and variables under private
	private:
	int totalred,totalgreen,totalblue;
};


here is the problem:
I created one object of the class world, so far no problem.
but I can not create an object of the class groundtile.
see function createbackgroundtiles() in the class world.
why is this?
I get these errors:

1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(54): error C2065: 'groundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(54): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(54): error C2061: syntax error : identifier 'groundtile'
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(55): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(55): error C2227: left of '->colorblue' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(56): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(56): error C2227: left of '->colorgreen' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(57): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(57): error C2227: left of '->colorred' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(58): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(58): error C2227: left of '->varredmin' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(59): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(59): error C2227: left of '->varredmax' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(60): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(60): error C2227: left of '->vargreenmin' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(61): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(61): error C2227: left of '->vargreenmax' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(62): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(62): error C2227: left of '->varbluemin' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(63): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(63): error C2227: left of '->varbluemax' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(64): error C2065: 'bravenewgroundtile' : undeclared identifier
1>c:\users\daniel\desktop\overige\coding\tutorial_sdl\sdl_project\sdl_project\main.cpp(64): error C2227: left of '->createself' must point to class/struct/union/generic type
1> type is ''unknown-type''
DeathoX 8
Experienced
Posts: 109
Joined: Sun Mar 27, 2011 12:56 pm

Re: C++

Post by DeathoX 8 »

Hi Rakiayn,
sorry to hear you stopped development of UnrealSpawn, it looked like it could have been very fun (although it would have required a lot of code indeed).

I have no experience with C++, but I'm pretty sure that undeclared identifier shows that the class/variable isn't declared when you're trying to access it (basically the compiler doesn't know it exists).
I think that class groundtile should be declared before you use it into void createbackgroundtiles(), since you're using that class in the function before it has been declared. Right now it comes afterwards.


I recall there was a way to declare a function at the start of the code so that you didn't have these problems, not sure if you can do that for classes too.
Sorry if I confused you more :P. Declaration and definition are two different things, in layman terms:

Declaring a function means saying to the compiler "Hey, there's a function called A that takes these parameters and returns this, you can use in the code!".
Defining a function means writing the actual function itself: "Hey, when function A gets called do this stuff".

You can do both at the same time, or you can declare the function at the start of the code and define it later :)


Don't quote me on this but I think some versions of C/C++ required you to declare all the variables at the start of the code. It's a good practice anyway IMO.
P.S. Iron Maiden! :rock:
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

Re: C++

Post by Rakiayn »

thnx for the advice.
I think you are right. but I cant seem to figure out how to do it. strangly enough I cant find my answer online
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

Re: C++

Post by Rakiayn »

after a lot of frustration and help from a friend I finally got it to work.
although the program crashes randomly after a while and I have no idea why.
I mst say unrealscript is a lot easyer :?
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: C++

Post by Feralidragon »

Rakiayn wrote:I mst say unrealscript is a lot easyer :?
It is. C++ is one of the most hardcore languages in the world, it's as close as assembly as a high level language can be.
You won't dominate it overnight, but since you started to play with it, you should invest a bit more time in understanding all the ins and outs about it, since they will also increase your own understanding about UEngine, and even perhaps other engines and stuff.
User avatar
UT Sniper (SJA94)
Inhuman
Posts: 753
Joined: Thu Jun 24, 2010 10:35 pm
Personal rank: Retard
Location: England
Contact:

Re: C++

Post by UT Sniper (SJA94) »

When i was learning C++, it was quite fun, challenging but fun, started to be able to use Polymorphism and other things, for some reason I just stopped for a while. If i didn't have family issues a while back i would probably be still doing it to this day.

Here is some C++ video tutorials, they are the best I have found around http://www.thenewboston.org/list.php?cat=16, in case you want to check some stuff out.
DeathoX 8
Experienced
Posts: 109
Joined: Sun Mar 27, 2011 12:56 pm

Re: C++

Post by DeathoX 8 »

Rakiayn wrote:after a lot of frustration and help from a friend I finally got it to work.
although the program crashes randomly after a while and I have no idea why.
I mst say unrealscript is a lot easyer :?
Sorry for not answering earlier as I was pretty busy, you probably found out yourself but moving the "class groundtile" part at the top of the function "createbackgroundtiles" should have solved those errors.

The bit I posted about earlier, separating the declaration and definition of a function/class, is called "forward declaration". I took a quick look and it looks like it isn't allowed for classes (it actually is but it is very very limited, can't be used normally) but only for functions.

For classes, I recommend adding separate public classes in different files and using the #include <file>.h in your main file.
For example you created a new file groundtile.c where you added all the class code, and a file groundtile.h where you specified that the class groundtile existed (google for "c++ header files" to learn how to write them). Then you just need to add an "#include groundtile.h" at the top of the class where you use groundtile and it will be available to use.

I agree with Feralidragon that C++ is very hard. If you just want to experiment a bit with code you could try out Java instead, which personally I found much easier (since it has automatic memory handling, complete official documentation and tutorials, is multi-platform, has simpler header/inclusion stuff, has some very simple to use standard user interfaces) although it has its downfalls (performance is worse which makes it less suited to games, it requires a Java Virtual Machine to be installed to make programs run).
I messed a bit with coding myself and when I want to make a simple program for fun I always use Java (I use the NetBeans IDE).
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

Re: C++

Post by Rakiayn »

Feralidragon wrote:
Rakiayn wrote:I mst say unrealscript is a lot easyer :?
It is. C++ is one of the most hardcore languages in the world, it's as close as assembly as a high level language can be.
You won't dominate it overnight, but since you started to play with it, you should invest a bit more time in understanding all the ins and outs about it, since they will also increase your own understanding about UEngine, and even perhaps other engines and stuff.
it cereteinly is. especially stuff like pointers is hard to get.
I am still doubting to switch to java or not
UT Sniper (SJA94) wrote:When i was learning C++, it was quite fun, challenging but fun, started to be able to use Polymorphism and other things, for some reason I just stopped for a while. If i didn't have family issues a while back i would probably be still doing it to this day.

Here is some C++ video tutorials, they are the best I have found around http://www.thenewboston.org/list.php?cat=16, in case you want to check some stuff out.
those are some nice video's
thanks!
DeathoX 8 wrote:
Rakiayn wrote:after a lot of frustration and help from a friend I finally got it to work.
although the program crashes randomly after a while and I have no idea why.
I mst say unrealscript is a lot easyer :?
Sorry for not answering earlier as I was pretty busy, you probably found out yourself but moving the "class groundtile" part at the top of the function "createbackgroundtiles" should have solved those errors.

The bit I posted about earlier, separating the declaration and definition of a function/class, is called "forward declaration". I took a quick look and it looks like it isn't allowed for classes (it actually is but it is very very limited, can't be used normally) but only for functions.

For classes, I recommend adding separate public classes in different files and using the #include <file>.h in your main file.
For example you created a new file groundtile.c where you added all the class code, and a file groundtile.h where you specified that the class groundtile existed (google for "c++ header files" to learn how to write them). Then you just need to add an "#include groundtile.h" at the top of the class where you use groundtile and it will be available to use.

I agree with Feralidragon that C++ is very hard. If you just want to experiment a bit with code you could try out Java instead, which personally I found much easier (since it has automatic memory handling, complete official documentation and tutorials, is multi-platform, has simpler header/inclusion stuff, has some very simple to use standard user interfaces) although it has its downfalls (performance is worse which makes it less suited to games, it requires a Java Virtual Machine to be installed to make programs run).
I messed a bit with coding myself and when I want to make a simple program for fun I always use Java (I use the NetBeans IDE).
yes I created separate files for the classes, thnx.
User avatar
Rakiayn
Masterful
Posts: 550
Joined: Fri Aug 28, 2009 3:33 pm

Re: C++

Post by Rakiayn »

I decided to switch to java and converted my program to java.
I must say it is a lot easyer, no trouble with pointers, memory leaks and asigning objects.
Im glad I did
Post Reply