Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Some things I don't understand about C++, classes, scope, functions...

Author
Message
Mazuru
16
Years of Service
User Offline
Joined: 28th Feb 2008
Location:
Posted: 15th Sep 2009 23:08
I've just started dabbling in C++ and Dark GDK, and it's really confusing the heck out of me...

I'm not too far into my project, it's just going to be a simple 2D sidescroller like Mega Man, and I've hit a roadblock.

I have 3 cpp and header files, "main, player control, and maps". The "main" files are supposed to tie everything together, player control is for handling input and moving the player sprite, and maps is for generating maps.

So, I've successfully made a map and the player can move with the arrow keys, but now that I need some collision detection... I don't know where to put the code for it. If I put the code for it in the player_control() function in the player control.cpp file, it can't access("undeclared identifier") the map class that was made in the make_map() function in the maps.cpp file which stores all of the data about my maps, which is quite important since I need to figure out which tiles are what (are they solid? are they just background? ladder?). If I put the collision code in the map source file, it can't access data about the player.

I'm so confused and I just don't know what to do. This was so easy in DBPro... in there I would just slap a bunch of global variables in and make tons of functions being run in a single loop... everything could access any of the variables no matter where they were, but this just doesn't work in C++...
Paynterboi TicTacToe
16
Years of Service
User Offline
Joined: 23rd Dec 2007
Location: That one place
Posted: 16th Sep 2009 00:10 Edited at: 16th Sep 2009 01:06
Ok I'm a noob as well but what you are saying seems to me that you are using functions stored in other .cpp files. I believe to use external functions you code them into a .h file and #include "file.h" at the beginning of your c++ app. I'm not 100% sure becasue my first project ( ultimate tic tac toe ) is all coded within one .cpp file.

[edit] I found this on youtube...
http://www.youtube.com/watch?v=L0yId__hBQA

It gives info about how to use functions that are in .h files in a main .cpp file

Also I did find info about useing functions from external .cpp files and although i dont know how to do that yet I would still reccomend moving everything into the one .cpp file at least until you become more familliar with external .cpp files.

If you haven't already, use these referenses

http://www.cplusplus.com/doc/tutorial
http://www.cprogramming.com/tutorial.html
http://www.learncpp.com/cpp-tutorial/03-introduction-to-cc

or the MSDN site.

pikahsoh@hotmail.com
email me if you have any noobish questions that you think I can answer.
[edit] this email also works for yahoo messanger AND msn messenger.

Quote: "in there I would just slap a bunch of global variables in and make tons of functions being run in a single loop... everything could access any of the variables no matter where they were, but this just doesn't work in C++... "


to gain some understanding about the scope of variables in c++ use the first site, find the variables link, and scroll down until you see a section outlined by a bold black line explaining scope of variables. Also, I found that making a thread so that people can follow along and answer questions specific to your project while provideing code snippets and uploading up-to-date source codes/exe/required media will get you a bunch of help and you will get explinations to questions that are much simpler to understand as opposed to online tutorials.

EYE R T3H +ick +ack +oe mester
BearCDP
15
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 16th Sep 2009 02:06
If you're using a global variable in C++ (not as common a practice as in DBPro) you need to make sure that the current CPP file you're wanting to compile (the one that's #including a header file that corresponds with the other CPP file that has the global variable) is aware of global variables declared in other source files by using the "extern" keyword. The reason for this "extern" thing is because what happens when you declare a global variable twice in two different source files?

Here's a real nonsense simple program to illustrate the concept, it should theoretically compile and run and print "myGlobalVar = 42". But . . . I'm not perfect, and not on my partition with the compiler. If you're having trouble understanding I can make sure this compiles and give a more full-fledged example.

"inclExample.h"


"inclExample.cpp"


"main.cpp"
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 16th Sep 2009 06:31
Wrong way around, you declare the 'real' variable in a source file otherwise anything including the header will cause multiple instances of the variable to be created in the same scope thus give you an error. You just need to do 'int whatever;' in your .cpp file then 'extern int whatever;' in the header.

However you shouldn't really need to use extern that much, it's generally indicative of bad engine design if you need a lot of things in the global scope. Proper use of classes should allow you to branch everything out from the entry point thus making code significantly simpler to manage/read.

BearCDP
15
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 17th Sep 2009 03:15
Whoops, I figured I'd mess something up.

You can even avoid the nastiness of "extern" altogether. Like dark coder said, you should be encapsulating all your data anyway, and delegating tasks to objects. If you absolutely need a few global variables though, you can create a "GlobalData" class with the variables as static members.

What sort of data do you want to make global?
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Sep 2009 22:49
Quote: "I have 3 cpp and header files, "main, player control, and maps". .... If I put the code for it in the player_control() function in the player control.cpp file, it can't access("undeclared identifier") the map class that was made in the make_map() function in the maps.cpp file ... If I put the collision code in the map source file, it can't access data about the player."


You say the player and map files can't access each other, but why not? If you have a main file, then you surely have such statements in your main file:



Nothing prevents you from doing the same in other files as well. If player.cpp needs something from map.cpp, then put the
statement in the beginning of player.cpp and it will see everything that map.h makes available. And the other way around. They can even include each other, although that's not a recommended practice but possible. Otherwise, it's a design question how classes interact with each other in the program.
Mazuru
16
Years of Service
User Offline
Joined: 28th Feb 2008
Location:
Posted: 18th Sep 2009 03:26
When I do that, Mireben, I get boatloads of errors, most of which are "____ is already defined in main.obj". If I include all of the header files in main.cpp, the variables won't be usable in maps.h or player.h, so I get all of those errors when I also include maps.h in maps.cpp and player.h in player.cpp

I'm just going with a single file for now until I get more experience in C++, then worry about organizing into multiple files later.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 20th Sep 2009 12:22
The "already defined" errors can be easily fixed by either using the
line in the beginning of the header files, or putting the content of the header into an #ifdef block. Either of these tells the compiler not to read the header more than once.

http://en.wikipedia.org/wiki/Pragma_once
ericshane
15
Years of Service
User Offline
Joined: 6th Oct 2009
Location:
Posted: 7th Oct 2009 06:48
Hi, I am also new to Dark GDK, however, I have a pretty thorough grounding in C++. I know that you have probably fixed your problem and moved on, however, I wanted to add my own ideas to this thread. As Mireben said, "already defined" errors can be fixed by either placing "#pragma once" as the first line in each header file(If using Visual Studio, at least. I don't know if that works for other compilers.) or arranging your header files like this:

#ifndef UNIQUEIDENTIFIER
#define UNIQUEIDENTIFIER

header contents
...
...

#endif

UNIQUEIDENTIFIER can be any unique name, although most people use the name of the header file in all caps followed by "_H", such as using "#ifndef TEST_H" for the header file test.h.

Also, as dark coder said, all of your data should be encapsulated as classes. This keeps everything neat and only allows functions to access the data they require. If you place everything into classes, use one of the two methods I mentioned earlier to prevent "already defined" errors, and #include the header files that each .cpp file needs, you should have no problems.

Always Seeking Education
ericshane

Login to post a reply

Server time is: 2024-11-20 15:34:03
Your offset time is: 2024-11-20 15:34:03