.com.unity Forums

.com.unity Forums (http://forum.shrapnelgames.com/index.php)
-   Space Empires: IV & V (http://forum.shrapnelgames.com/forumdisplay.php?f=20)
-   -   OT: Stupid c++ (Roguelike development) (http://forum.shrapnelgames.com/showthread.php?t=29767)

narf poit chez BOOM August 18th, 2006 06:11 PM

Re: OT: Stupid c++
 
Apparently, the compiler tries to treat every .cpp file as a seperate program, so multiple .cpp files do not work. I presume it works with other compilers.

PvK August 18th, 2006 06:31 PM

Re: OT: Stupid c++
 
C++ is trying to help you to help yourself to write nice code. Why do you want a class member function to have side effects on a global vector defined outside the class?

A nicer way to do it would be to have the constructor take a parameter which tells the object about the global vector (pass it a pointer or reference), store that pointer/reference and use that.

PvK

PvK August 18th, 2006 06:33 PM

Re: OT: Stupid c++
 
No, every compiler has limited file scope, to keep globals slightly under control. You can get to objects in other files by declaring them "extern", but relying on globalness tends to get ugly.

narf poit chez BOOM August 18th, 2006 06:41 PM

Re: OT: Stupid c++
 
AngleWyrm, what's that code for?
No, just a slight precedence error in 'characterToGraveyard'. I fixed it, but thanks anyway.

@Pvk: Wouldn't that take a lot of memory?

Ah, thanks. The advice makes more sense now.

Update, call it: Simple Roguelike v0.1.4.5

Not a single pointy in the code.

What kind of file is 'main.o'? It's rather big - Can I delete it?

PvK August 18th, 2006 08:07 PM

Re: OT: Stupid c++
 
It'll use all of 4 bytes if you make it a class static data member, in which case there is only one per class, not per object:

static (STL::vector<cheese>)* pGlobalCheeseVector;

However I guess I'm harping object-oriented design when you are working in a good-ol' C hack design paradigm, which may actually be less confusing for you at this point. If it works and you understand why, it's good code. http://forum.shrapnelgames.com/images/smilies/wink.gif

main.o is probably an object file. That is, an intermediate file before building the exe. If you delete it, it just means it the compiler will need to recreate it before building the exe, even if you hadn't changed any code in main.cpp before you rebuilt. Generally you can ignore main.o, and you don't need to distribute it to players.

PvK

narf poit chez BOOM August 18th, 2006 08:20 PM

Re: OT: Stupid c++
 
Ah, thanks.

Object-oriented design is what I'm trying to do. Any difficulties come because I'm self-taught (Aside from what random people on the internet have taught me).

Er, I've tried to make a pointer to a vector, but it didn't work.

Death to globals!

PvK August 18th, 2006 08:33 PM

Re: OT: Stupid c++
 
Hmm, a pointer to vector should work - what syntax are you using?

narf poit chez BOOM August 18th, 2006 09:09 PM

Re: OT: Stupid c++
 
I dunno. Whatever syntax Dev c/c++ uses. I found a syntax page, but it doesn't list the type.

I'll try it again, soon as I finish getting rid of the current global.

(Er, why did you type STL::vector? I thought it was std?)

This seems to work: 'static std::vector<Character> * CVector;'

So I guess I just put my &Character list vector into CVector, then access that from inside the class?

PvK August 18th, 2006 10:05 PM

Re: OT: Stupid c++
 
Oh, sorry STL->std (bad human memory).

By "what syntax", I just meant what does your code look like. Yes, the line you wrote is the syntax I was trying to remember and suggest to you. Good job. http://forum.shrapnelgames.com/images/smilies/wink.gif

Yes, so the syntax for that would be something like:
<font class="small">Code:</font><hr /><pre>
// In the class definition file e.g. NarfClass.h
class NarfClass
{
// Constructor:
NarfClass( std::vector&lt;Character&gt; * pCVectorToUse );

// Method that does something with CVector:
void DoStuffWithCVector();

// The static storage of the Character Vector:
static std::vector&lt;Character&gt; * pCVector;
};</pre><hr />

<font class="small">Code:</font><hr /><pre>// In the class definition file e.g. NarfClass.cpp
#include "NarfClass.h"

NarfClass::NarfClass( std::vector&lt;Character&gt; * pCVectorToUse )
{
pCVector = pCVectorToUse;
}

void NarfClass::DoStuffWithCVector()
{
int NumberOfCharactersInGame = pCVector-&gt;size();
// etc...
}</pre><hr />

<font class="small">Code:</font><hr /><pre>// In the class that stores the whole gamestate:
#include "NarfClass.h"

// The actual vector:
static std::vector&lt;Character&gt; CVector;

// The creation of a NarfClass object:
NarfClass NarfsNarf( &amp;CVector );

// Now NarfsNarf is an object that can do stuff with CVector.
NarfsNarf.DoStuffWithCVector();</pre><hr />
I may have a typo or be missing something, but that's about how I'd do it.

PvK

narf poit chez BOOM August 18th, 2006 10:15 PM

Re: OT: Stupid c++
 
Thanks again.

For syntax, I tend to go with the needs of the moment.

So, you mean make a handler class for the Character class?

How do you declare something 'extern'?


All times are GMT -4. The time now is 05:00 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©1999 - 2025, Shrapnel Games, Inc. - All Rights Reserved.