Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 22 23 [24] 25 26 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100679 times)

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Logged

malloc

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #346 on: November 26, 2011, 08:37:23 am »

Hehe, well, I am a very uncreative guy, and therefore often takes my nicknames from C/C++ commands. =)
Logged

Sergius

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #347 on: November 29, 2011, 11:21:37 am »

Alright, I've started programming C++ after years of not touching the stuff. Took me a bit to get the hang again of templates and pointers and stuff, but here's one thing that's nagging me:

I've got an object definition (let's call it obj.cc and obj.h). In obj.h is the class declaration, and for instance, it uses both the "<iostream>" and the "<list>" libraries (one of the member variables is a list of pointers to another class).

Now, let's say I need to use said class in "main.cc". So I include obj.h... but it complains because it doesn't recognize the type "list<>". Even though the list is in the private members and never actually used by main.cc directly. So I have to include "<list>" in every c++ file if it includes said header.

So a workaround would be to just include <list> in my header, so that other c++ files don't complain. I don't know if this is a good practice... besides, if I ever actually need to use a "list<>" class in said c++ file, should I #include <list> anyway even though it's not required anymore (since it's included indirectly) just because it's good manners?

NOTE: The same thing happens with the type std::string, but since iostream seems like a common and useful library anyway including it isn't such a hassle.

Or should I just give up and include some sort of precompiled header with all the libraries I'm using?
« Last Edit: November 29, 2011, 11:23:42 am by Sergius »
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Help Thread (For Dummies)
« Reply #348 on: November 29, 2011, 02:21:31 pm »

Well, it's basically that the compiler needs to know the size of a list<> so that it knows the size of the class itself, as well as the various offsets to other values (or so I would assume).

To make circular references possible, if all you're using from a given header is a pointer, you can use a prototype to tell the compiler that it is, indeed, a class, that may be defined later/elsewhere.

However, I believe that the common solution is simply to include the extra header, but not having any other file rely on it being included there so that nothing breaks if you decide to take it out later. It should have include guards of some sort so that including it twice doesn't cause any errors, and more recent compilers can be very good at optimizing out the extra includes, so it would likely just add an imperceptibly short extra delay when compiling files that wouldn't have included that header anyway.
Logged
Eh?
Eh!

Sergius

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #349 on: November 29, 2011, 03:22:16 pm »

Thanks for the tip.

I actually found an article that kind of explains to me what to do and why do we normally think that putting includes in headers is bad:
http://www.cplusplus.com/forum/articles/10627/

Basically, it's not bad. Just that you have to use a "guard" so a header doesn't get included twice. So yeah, if a.h needs b.h, it makes no sense to avoid putting #include "b.h" directly in a.h.

And yeah, basically if my .cc file needs the header to use part of it *directly*, include it just in case it's later taken out of the other one, like you said.

Also Eclipse apparently has no inbuilt support for PCHs, but that's not very important.
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #350 on: November 29, 2011, 03:31:26 pm »

I didn't really read through the subject and thus didn't really notice what's up, but since it's about headers and includes, I guess an #ifndef should help.
Logged

malloc

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #351 on: November 29, 2011, 04:49:13 pm »

I've got an object definition (let's call it obj.cc and obj.h). In obj.h is the class declaration, and for instance, it uses both the "<iostream>" and the "<list>" libraries (one of the member variables is a list of pointers to another class).

Now, let's say I need to use said class in "main.cc". So I include obj.h... but it complains because it doesn't recognize the type "list<>". Even though the list is in the private members and never actually used by main.cc directly. So I have to include "<list>" in every c++ file if it includes said header.

If you have an object declaration in a header, how did you even manage to get an unrecognized type? I mean, to declare a list type member in a class, you would need to include the list class from standard library in the object definition header.

As for your library question, well, some people actually do something like this, they have one header, in which they include all STD classes and libraries, then just include this header in all files. Your compiler should be smart enough to only have to compile this header once, as this should never really change.
Also, good practice comes with actually working with the language. It does not really make sense to try and force yourself to write perfect code from the get go, that way you will spend way to much time writing good code, and to little time actually making something useful.

Good practice is usually defined by structuring your program appropriately. This means that you start out with a lot of primitive data types, say iterators, smart pointers and whatever. The important part is that these primitives often do not have any dependencies. Then you slowly add more complex objects from these primitives, like queues, lists and vectors etc. (A lot of these primitives are already in the STD library, so writing them yourself for anything other than practice is redundant.)
Also, good design usually also means as few dependencies as possible, keep your code modular and objective. While trying to avoid pitfalls like singletons and other annoying dependency chains.

But always remember, do not focus on writing perfect code at first, try at first just to get things done, get familiar with the language and OO-programming.
Logged

Sergius

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #352 on: November 29, 2011, 05:31:38 pm »

If you have an object declaration in a header, how did you even manage to get an unrecognized type? I mean, to declare a list type member in a class, you would need to include the list class from standard library in the object definition header.

You can include the list class from standard library in any C++ files that include the definition header.

Quote
As for your library question, well, some people actually do something like this, they have one header, in which they include all STD classes and libraries, then just include this header in all files. Your compiler should be smart enough to only have to compile this header once, as this should never really change.

I'm pretty sure you have to add the #IFNDEF/#DEFINE statements for the compiler to know it only has to compile the header once. This fortunately comes by default on the header files generated automatically by Eclipse. But yeah, having a single header with all the standard classes is an option.
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #353 on: November 29, 2011, 07:42:37 pm »

Edit: Fixed previous problems. Go me!
« Last Edit: November 29, 2011, 08:01:40 pm by MaximumZero »
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

malloc

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #354 on: November 30, 2011, 03:17:44 am »

You can include the list class from standard library in any C++ files that include the definition header.
What? Yes, that I what I meant. Then it should not be possible to have an undefined type, because including a header you automatically include any other headers. Are you defining classes in some other way than this:
Code: [Select]
//classdef.h
#include <list> //Or some header that makes use of list classes
struct obj{
     std::list<void*> something;
};
Code: [Select]
//classinaction.cpp
#include "classdef.h"
void somefunction(const obj & ref){
     obj.something.pushback((void*)NULL);
};
I'm pretty sure you have to add the #IFNDEF/#DEFINE statements for the compiler to know it only has to compile the header once. This fortunately comes by default on the header files generated automatically by Eclipse. But yeah, having a single header with all the standard classes is an option.
Those macros are a given, but they only ensure classes or definitions are not defined more than once.
Also, knowing how headers work it actually does not matter how many times and where you include std headers, they are not really "linked" to your compiled program before you use some classes from them.
Having a standard header that includes all of STD is just a convenience, some don't like it because it can lead to some clutter when developing big apps, but that is what namespaces are for.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #355 on: November 30, 2011, 04:50:53 am »

Also, knowing how headers work it actually does not matter how many times and where you include std headers, they are not really "linked" to your compiled program before you use some classes from them.
Having a standard header that includes all of STD is just a convenience, some don't like it because it can lead to some clutter when developing big apps, but that is what namespaces are for.

I tend to make it so each file includes what it needs rather than have a 'generic' include file of some kind, this is mostly to make it so I can see exactly what is used and allow more isolation for unittests.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

malloc

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #356 on: November 30, 2011, 05:04:55 am »

I tend to make it so each file includes what it needs rather than have a 'generic' include file of some kind, this is mostly to make it so I can see exactly what is used and allow more isolation for unittests.
As I said, some people find it adds clutter to their code, which is totally understandable. But it does not really make any difference having a generic include file or personally include the code in each file if you structure your program accordingly.

I tend to include all of STD libraries for convenience, as I find the standard classes really convenient. On the other hand, I never really let namespaces scope into the global scope.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #357 on: November 30, 2011, 05:46:29 am »

I never really let namespaces scope into the global scope.

Should certainly never be done in a header.
One of the previous companies I worked for did this with four or five namespaces, in a generic header no less, and it made adding third-party libraries a nightmare. Of course that company also had their own stl implementation and a custom null type too....
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #358 on: December 10, 2011, 01:08:53 pm »

Python namespace kind of question here!

I've been playing around with a game idea, and I'm having trouble understanding how python does its namespace/module/package system.

I've got a directory structure of

Code: [Select]
./lib/
./lib/monsters/
./lib/monsters/__init__.py   #contains some important methods.
./lib/monsters/ai/
./lib/monsters/ai/__init__.py   #empty
./lib/monsters/ai/ai1.py
./lib/monsters/ai/ai2.py
./lib/monsters/definitions/
./lib/monsters/definitions/__init__.py   #empty
./lib/monsters/definitions/def1.py
./lib/monsters/definitions/def2.py
 

Now, in my monsters/__init__.py I've got something like:

Code: [Select]
#loading AI's
from ai.ai1 import *
from ai.ai2 import *

#Loading monsters
from definitions import def1

spawnlist = []

def add_def(mondef):
global spawnlist
spawnlist.append(mondef)


def spawn(name, x, y):
#bunch of code that works fine

I'm trying to call add_def from within the ./lib/monsters/definitions/def1.py so that as I add monster definition files they can automatically add themselves to my list, but it always gives an error of "AttributeError: 'module' object has no attribute 'add_def'". 

So I'm obviously misunderstanding something.  Does anybody know a more proper way to access that spawnlist?   Or advice on a more pythonesque way of doing this?
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #359 on: December 11, 2011, 02:08:40 am »

Ah, I seem to have jumbled something together that works. 
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout
Pages: 1 ... 22 23 [24] 25 26 ... 91