Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Help wanted. C++ question.

Name: Anonymous 2009-07-13 17:49

How do I declare a global object 'within' the class's brackets?

class Box{

public:
int x, y;
//Here's the problem.
Box A;//?

bool collision(Box A, Box B){}

}A, B;

Is it even possible to declare global objects within a class? Yes/no? Depending on your answer I may:
 
a) Drop out of college and become a plumber.
b) Kill myself.
c) Finish the app I'm currently working on.

Name: Anonymous 2009-07-13 17:51

I have no idea why I just burst out laughing, does my subconscious know I'm being trolled?

Name: Anonymous 2009-07-13 17:56

>>2
Yeah, I'm new to coding. Give me a break with this one man. I stayed up for 15 hours googling every where for an answer to my problem. Also, programming sucks. Stay in school kids and never become a programmer. If you're a masochist, then programming is the right job for you. Is it C++, or are all languages this annoying? C++ refuses to listen to my god tier human logic. And yes, my logic is always perfect. Humans don't make mistakes, the languages do.

Name: Anonymous 2009-07-13 18:01

b)

Name: Anonymous 2009-07-13 18:06

I really don't know what the fuck you're trying to do with that snipper but C++ does not work that way!

Name: Anonymous 2009-07-13 18:08

>>5
s/snipper/snippet/

Name: Anonymous 2009-07-13 18:08

>>1
Hint:  Use pointers.

Name: Anonymous 2009-07-13 18:09

>>3
Is it C++,
Yes.

or are all languages this annoying?
No.

Also, I hope you're trolling. Global objects within a class? What the shit are you trying to do here? It makes no goddamn sense to my actual, working human logical facilities.

Name: Anonymous 2009-07-13 18:13

>>7
More hints please. Can you give me a sample? I'm practically begging you.

>>8
Have faith Anon. Despite how retarded it might sound, it serves a purpose.

Name: Anonymous 2009-07-13 18:17

I'm feeling generous. Pastebin all of your .hs/.cpps and I'll try to fix it for you (if it's fixable)

Name: Anonymous 2009-07-13 18:39

Name: Anonymous 2009-07-13 18:42

7 7 7 is my name
7 come
and 7 go
and
7
still remaiiiiin
aaaaaaaaaaaaaaiiiiiiii

Name: Anonymous 2009-07-13 18:45

>>11
The library I'm using is Allegro.

I'm basically trying to move a box sprite into another box sprite while checking for collision. But I cant seem to use my 'collision' function within other members of my 'Box' class.

Name: Anonymous 2009-07-13 19:20

This code is not tested or guaranteed to compile.

http://pastebin.com/m11f1a268

Name: Anonymous 2009-07-13 19:32

>>14
Sadly, I'm a complete rookie at reading other peoples' code (even when it's an alteration of my own code).

It's going to take me (at least) 20 minutes to digest this code. I'll let you know if it helped. But hopefully after reading my code you realize the importance of being able to declare global objects within a class.

Personally, I enjoy placing every/any code related to my class within the class itself. It's neater that way.

Name: Anonymous 2009-07-13 19:40

>>15
But hopefully after reading my code you realize the importance of being able to declare global objects within a class.
Uh, no. I realize the importance of good design; i.e. anything but what you did.

Name: Anonymous 2009-07-13 19:48

I sadly don't know what 'Box &box' means/does.

I never really payed attention to pointers, addresses, and objects.

Name: Anonymous 2009-07-13 19:55

>>17
It means box is a reference parameter of type Box. This avoids copying box when you pass it to this function. It actually should have been as follows, since box should not be getting changed.

bool collision_with(const Box &box)

Name: Anonymous 2009-07-13 20:00

>>18
And really, the whole function probably should have been const as well.

You'd better learn these things if you want to be programming in C++.

Name: Anonymous 2009-07-13 20:10

>>19
Thank god I don't want to be programming in C++.

Name: Anonymous 2009-07-13 20:15

C++ is the worst designed programming language in the history of mankind. It's completely useless and C++0x will make the situation even worse, lol.

Name: Anonymous 2009-07-13 20:18

>>20
Amen. It's not like I'm recommending this garbage. I'm just saying that you're not going to write working, low-bug programs in C++ without knowing how the thing works. You're not going to do that anyway, but you'll come a lot closer if you know what you're doing.

Name: Anonymous 2009-07-13 20:20

>>18
Oh god, it actually worked. To be honest with you, your code itself didn't help, but the &box thing (somehow) managed to do the trick. I still don't know what the heck it's doing, but my code is actually working now:

bool collision(Box &a, Box &a){}

I could kiss you right now. I guess I should learn more about addresses and pointers before advancing any further. Some how I've managed to survive up to this point with little or no help from my peers. High-school, college, and universities are useless, hence the reason why I taught myself Algebra, calculus, and some C languages. But C++ is finally starting to get to me. Everyday there's a new bug to fix or some minor limitation to overcome. To put this in layman's terms, C++ has taken a giant shit in my pipe-dream. Now the pipes to my dreams are clogged. I feel so lonely in my battles against C++. If this fails, what else could I do with my life?

There should be some type of 'free' Internet tutor guys that go around helping people. Because our current system isn't working. As of right now there's really only two options for becoming a competent programmer:
1). Go to some shitty university and read from text books.
2). Waste months out of your precious (early) 20s becoming a neck-beard while everybody else your age is outside having fun during these crucial years of our development. 

/life story

Name: Anonymous 2009-07-13 20:23

>>23
bool collision(Box &a, Box &a){}
Did you actually list two parameters with the same name?

Name: Anonymous 2009-07-13 20:26

>>24
Oops, copypaste error.

bool collision(Box &a, Box &b){}

Fixed

Name: Anonymous 2009-07-13 20:29

>>24
Disclaimer: I haven't slept in a day. I'm surprised there weren't more errors in my post. Or maybe I'm just too sleepy to notice them.

Name: Anonymous 2009-07-13 20:29

>>25
Good. But let me point out again that this method should only take one parameter. Why would you pass Box a to itself?

Name: Anonymous 2009-07-13 20:38

>>27
Well, I altered it for two boxes, I think? Box a & b?

Again, I still don't have the slightest inkling of what my code is doing. And to be honest with you, I couldn't care less. If it's not broke; don't fix it. Put yourself in my position, imagine if you spent the last few days trying to fix something and all of a sudden it worked.

Name: Anonymous 2009-07-13 20:48

Well, thanks again for your help. I'm going back to work on my project. Just remember if a game on par with Cavestory comes out in two years, you deserve some of the credit for it. I was seriously about to quit my project and spend the next few months working on my CGI programs.

Name: Anonymous 2009-07-13 21:15

>>28
So, box A and box B:
A.collide_with(B);. Why do you duplicate A? The collision method is a member of box A, so it receives box A as an implicit parameter.

If it's not broke; don't fix it.
The point is that it's broke. What I think you're doing is making the classic is-a/has-a confusion. A and B are boxes, they don't have boxes. You don't have a box class with storage for instances of itself, and you don't pass objects to themselves since they are themselves.

A class is not a collection of code for dealing with some particular variety of bullshit; a class is a description of the form an object is going to take. Why is a box object going to have a bunch of box variables inside it? Does that have anything to do with the nature of a box: to represent a rectangle? Likewise, an object's methods are going to involve that object, and are not going to have that object passed to it. Only pass data that doesn't already exist in the object, such another object to collide with.

Name: Anonymous 2009-07-13 21:26

I'd just like to interject to say that ``references'' in C++ are useless. Please, use pointers only.

Name: Anonymous 2009-07-13 21:45

>>31
Why the hell did he include them anyway?

Name: Anonymous 2009-07-13 21:56

>>32
DIX

Name: Op 2009-07-13 22:27

>>30
Alright, I finally realize I've been doing it wrong for all of these years. Only now do I realize I had a bastardized interpretation of C++ classes, objects, and functions. I've always thought of the class itself as one giant object.

Thanks for the enlightenment brodo. And just for the record, even though I'm new to C++ I've been coding on and off for a few years. I was never really stuck on this project, nor have I ever been stock on any of my projects in terms of syntax and logic.

Being the blatant neat freak that I am, I amateurishly tried to stuff everything related to my Box class within the Box class, with little regard to common sense.

Now let's pretend like this thread never happened, alright? And by that I mean, let it die miserably. Lesson learned, C++ is significantly more complex than some of the other user friendly languages out there. You cant just start dicking around with it - hoping that it'll work. Next time I'll allocate more than two weeks of study time to learn a new language.

Name: Anonymous 2009-07-13 22:31

>>34
nor have I ever been *stuck on any of my projects in terms of syntax and logic.

Name: Anonymous 2009-07-13 22:40

You know what, nobody actually told you how to declare global variables in a class:

class someclass {
public:
static int variable;
};

now, you have someclass::variable available to you wherever you have access to someclass, and it is the same variable for all instances of someclass. In Java, this is someclass.variable.

Name: Anonymous 2009-07-13 22:46

>>36
Forgot: that is the ONLY way to declare a global variable in Java.

Name: Anonymous 2009-07-13 22:59

>>36
Out of curiosity, would it be possible to declare global objects like >>1

Not that I care. Doing such would probably contradict the purpose of objects.

Name: Anonymous 2009-07-13 23:26

>>38
Exactly like >>36 did. Static members are available even when there are no instances of the class and they are shared by all instances of the class. They are basically just global variables inside the class's namespace.

Name: Anonymous 2009-07-14 0:18

>>31
On the contrary. They cannot be null¹, allow simpler notation (in some cases), throw exceptions in dynamic_cast instead of yielding null, simplify passing inout parameters, and can't be left uninitialized if they're a member of a aggregate.

I don't mean to say that they're a good thing, just that they're not entirely useless, and do have a purpose.

¹ Unless you're an idiot.

Name: Anonymous 2011-02-04 13:00

Name: Anonymous 2011-02-04 15:10

Don't change these.
Name: Email:
Entire Thread Thread List