C++ is JIBBERISH

C++ is JIBBERISH

General — Page [1]
CornJer
Metal does cocaine.

Frontline Heroism Medal
2005 Mar 21 • 1531
36 ₧
Jesus, I've been trying to learn it, and the more and more I try, the more and more it looks like Durkadurkanese.

What the hell is this?
[code:1]
LONG lInService = FALSE; // reentrancy semaphore

BOOL AudioStream::ServiceBuffer (void)
{
BOOL fRtn = TRUE;

// Check for reentrance
if (InterlockedExchange (&lInService, TRUE) == FALSE)
{ // Not reentered, proceed normally
// Maintain elapsed time count
m_nTimeElapsed = timeGetTime () - m_nTimeStarted;

// Stop if all of sound has played
if (m_nTimeElapsed < m_nDuration)
{
// All of sound not played yet, send more data to buffer
DWORD dwFreeSpace = GetMaxWriteSize ();

// Determine free space in sound buffer
if (dwFreeSpace)
{
// See how much wave data remains to be sent to buffer
DWORD dwDataRemaining = m_pwavefile->GetNumBytesRemaining ();
if (dwDataRemaining == 0)
{ // All wave data has been sent to buffer
// Fill free space with silence
if (WriteSilence (dwFreeSpace) == FAILURE)
{ // Error writing silence data
fRtn = FALSE;
}
}
else if (dwDataRemaining >= dwFreeSpace)
{ // Enough wave data remains to fill free space in buffer
// Fill free space in buffer with wave data
if (WriteWaveData (dwFreeSpace) == FAILURE)
{ // Error writing wave data
fRtn = FALSE;
}
}
else
{ // Some wave data remains, but not enough to fill free space
// Write wave data, fill remainder of free space with silence
if (WriteWaveData (dwDataRemaining) == SUCCESS)
{
if (WriteSilence (dwFreeSpace - dwDataRemaining) == FAILURE)
{ // Error writing silence data
fRtn = FALSE;
}
}
else
{ // Error writing wave data
fRtn = FALSE;
}
}
}
else
{ // No free space in buffer for some reason
fRtn = FALSE;
}
}
else
{ // All of sound has played, stop playback
Stop ();
}
// Reset reentrancy semaphore
InterlockedExchange (&lInService, FALSE);
}
else
{ // Service routine reentered. Do nothing, just return
fRtn = FALSE;
}
return (fRtn);
}[/code:1]

Dont get me wrong, I get functions, switch statements, if-then, arrays, else, for loops, and all that good basic stuff, I just dont understand why people make shit tutorials on things like structures and classes, and other advanced-beginner things.

For example. lets look at multiple files. As far as I know, to include one file into another, you can use include "<Filename goes here>" for .cpp files, or header files, according to one tutorial, but another says they execute in the order they are listed in the makefile (whatever the hell that is)!

Another says that to declare a structure you can just go like this:
[code:1]
struct blah
{
int blahblah;
int blahblahblah;
int blahblehblah;
} blah1,blah2,blah3; //Like this
blah blah4;//Or this
[/code:1]
..yet another says you have to use a "new" statement to declare one.

And what kind of damn idiot trys to make us learn with cout and cin??? Its 2000-freaking-7 for christ sake, not 1985!

I dont know, maybe I'm a dumbass, and I'm not reading the tutorals right.
It just seems like every one of them out there is designed for 10 year old console applications, and/or is too complex or does not go into it enough.

Gawd.
[/rant]
If you jump high enough you'll hurt your ankles when you land.
 
 
 
2006 Nov 17 at 14:00 PST
SuperJer
Websiteman

2005 Mar 20 • 6629
Most tutorials out there really suck.

They like to just show you code, and not teach what it means.

I would be happy to answer C++ questions on the forums here. Be a nice change from all the same Hammer questions over and over again.

--------------------------------------------------

You should only use #include on header files, not .cpp files. It will include anything you tell it but you will paint yourself into a corner including .cpp files.

Using a makefile is one way to compile a C++ program. The makefile, among other things, lists all the .cpp files in the project.

In an dev environment like Visual Studio, the .cpp files for the project are listed in an explorer-window or something. It's the same thing.

#include <filename.h> is for STANDARD & LIBRARY headers only, like windows.h, stdlib.h, direct3d9.h, etc... If you want to include your own files you should use #include "yourfile.h"

--------------------------------------------------

Declaring a struct:

[code:1]struct MONSTER
{
int type;
int hitpoints;
int posx;
int posy;
}[/code:1]

Instantiating an object defined by a struct:

[code:1]MONSTER mon;
mon.type = 5;
mon.hitpoints = 100;[/code:1]
OR
[code:1]MONSTER *mon = new MONSTER;
mon->type = 5;
mon->hitpoints = 100;[/code:1]

cin and cout are stupid. You should be using printf and sprintf and fprintf.
 
 
 
2006 Nov 17 at 15:26 PST
molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Why not open a C++ section? Or maybe turn the Hammer (s and mallets) section into a Coding section or something.
LET LOVE REIGN
 
 
 
2006 Nov 17 at 15:38 PST
CornJer
Metal does cocaine.

Frontline Heroism Medal
2005 Mar 21 • 1531
36 ₧
Doesn't sound like a bad idea to me!

About multiple .cpp files; What determines what executes first? And how do you include aspects from other files, like class types and structure types?
If you jump high enough you'll hurt your ankles when you land.
 
 
 
2006 Nov 17 at 16:42 PST
SuperJer
Websiteman

2005 Mar 20 • 6629
.CPP files should only contain executable code in functions, and instantiations, and #includes. That's it.

.H files should only contain function prototypes, class/struct definitions, etc.

You should notice that .H files have only definitions and nothing that allocates memory and no instructions or anything like that. .H files are there to list off what all is in the associated .CPP file. The .CPP file contains all the actual working code, and code that allocates memory or actually creates something.

The main() function always executes first, no matter what .CPP it is in. You just have to have main() in one of your .CPPs.

Example:
[code:1]// doggie.h
#ifndef __DOGGIE_H__
#define __DOGGIE_H__

struct DOG
{
int legs;
float fur_length;
char fur_color[20];

DOG();
void chop();
};

#endif[/code:1]

[code:1]// doggie.cpp

#include <string.h>
#include "doggie.h"

DOG::DOG()
{
legs=4;
fur_length=6.1f;
strcpy(fur_color,"brown");
}

void DOG::chop()
{
legs--;
}[/code:1]

[code:1]// main.cpp

#include <stdio.h>
#include <stdlib.h>
#include "doggie.h"

int g_globe = 360;

int main()
{
DOG updog;
updog.legs = 5;
updog.chop();
printf("updog has %d legs and %f mm %s fur\n",
updog.legs, updog.fur_length, updog.fur_color);
system("pause");
}[/code:1]
 
 
 
2006 Nov 22 at 14:17 PST
CornJer
Metal does cocaine.

Frontline Heroism Medal
2005 Mar 21 • 1531
36 ₧
What if a class was defined in a .cpp file? Or a structure?

Or anything else for that matter?

Would it be accessable from other files?
If you jump high enough you'll hurt your ankles when you land.
 
 
 
2006 Nov 22 at 15:58 PST
SuperJer
Websiteman

2005 Mar 20 • 6629
It depends on your compiler, and the order it puts the files together but it SHOULD NOT be accessible from other files.

The class declaration ought to be in a header (.H) and the files that need to use it should #include the header.

-------------------------------------------------

Classes are just private structs by the way. Otherwise they are exactly the same:

[code:1]class XXX
{
public:
int a;
int b;
int func(int x);
}[/code:1]
is equivalent to
[code:1]struct XXX
{
int a;
int b;
int func(int x);
}[/code:1]
 
 
 
2006 Nov 24 at 20:17 PST
CornJer
Metal does cocaine.

Frontline Heroism Medal
2005 Mar 21 • 1531
36 ₧
There are structure member functions in C++?

Why use classes then, other than for inheritance?

One more thing: Why can't you declare strings in header files? Every time I do that in C++, it says that there is not a type called "string". If you can't do that, how do I make an external constant string, for, lets say, the location of a cfg file?
If you jump high enough you'll hurt your ankles when you land.
 
 
 
2006 Dec 9 at 15:22 PST
SuperJer
Websiteman

2005 Mar 20 • 6629
There is NO difference between a struct and a class except for public: vs. private:.


This works for me for storing a string in a header:

[code:1]// main.h
const char config_file[] = "c:\my.conf";[/code:1]


If you want to store it in a string class then I suppose you have to include the appropriate header (cstring?)

I've never used a string class in my life, though.
 
 
 
2006 Dec 11 at 14:24 PST
CornJer
Metal does cocaine.

Frontline Heroism Medal
2005 Mar 21 • 1531
36 ₧
Classes sound pretty stupid then.

Why limit yourself?

Finally; How on earth do extern variables work? I'm thinking that you need to define them like any other variable, and to use them in any other file, you simply declare "extern int x;".

Anywho, I wanna thank you sup's for being nice enough to answer me all my questions.

Fake edit: Why do you get linking errors if you define a constant in a header file?
If you jump high enough you'll hurt your ankles when you land.
 
 
 
2006 Dec 28 at 18:35 PST
CornJer
Metal does cocaine.

Frontline Heroism Medal
2005 Mar 21 • 1531
36 ₧
GTFO
If you jump high enough you'll hurt your ankles when you land.
 
 
 
2007 Jan 4 at 10:47 PST
NatureJay
SJA: Commander of Ruthless Abuse

Good Conduct Medal
2005 Mar 22 • 1871
574 ₧
There are currently 69 hours remaining to make the pornbot pay in spades.

Hurr hurr.
100% natural, no antibiotics, and bloodgrass-fed
 
 
 
2007 Jan 4 at 14:13 PST
CornJer
Metal does cocaine.

Frontline Heroism Medal
2005 Mar 21 • 1531
36 ₧
I get it.

69.

Heh. Heh heh.
If you jump high enough you'll hurt your ankles when you land.
 
 
 
2007 Jan 4 at 15:03 PST
Page [1]