void add_speed_direction (short direction, short addspeed, short& xspeed, short& yspeed) {
/*This function adds the desired speed to the speed of the entity that calls it, in
the direction it wants. This function should acquire its variables from the function
that calls it. I hope this is a legal operation within methods. Maybe it'll be
easier to make it a method itself? */
xspeed* += (addspeed*sin(direction));
yspeed* += (addspeed*cos(direction));
}
For some reason, it doesn't like my pointers and indirection operators. Microsoft Visual C++ 6.0 gives me this error when I go to compile it:
e:\docs\projects\space story\space story.h(124) : error C2059: syntax error : '+='
e:\docs\projects\space story\space story.h(125) : error C2059: syntax error : '+='
Name:
Anonymous2006-06-18 3:47
i think the problem is that xspeed and yspeed are already references and you don't need to include the *
Name:
Anonymous2006-06-18 4:53
Strange. But doesn't xspeed contain a pointer to the xspeed that was passed to it earlier? Wouldn't I then need the asterisk to tell it "Don't mess with the pointer, mess with the values pointed by this pointer?"
I mean, there are differences between references and deferences, or did I misread the tutorial?
Besides that speil, thank you, it works perfectly.
Name:
Anonymous2006-06-18 5:33
>>1
Doing it wrong. If you needed the dereferencing operators, they would be placed in front of the object they are dereferencing.
Name:
Anonymous2006-06-18 5:33
okay, here's the thing, you passed a INT by reference. Thus meaning that whatever you do to xspeed, will be changed once the function call is complete. What you're saying here is: derefrence the value inside xspeed and use that. Since your INT reference obviously does not contain a valid pointer, you'll get a nice runtime seg fault if it did compile. remove the * and you will be fine.
Name:
Anonymous2006-06-18 6:10
The contents-of operator goes before variables. In this case it doesn't go because variables aren't pointers but references.
Name:
Anonymous2006-06-18 13:03
OK... so it's like when I give it the pointer in the declaration statement, it automatically knows to take it as a reference, because it's being passed to a function... but why did the tutorial at http://www.cplusplus.com/doc/tutorial/functions2.html say I needed them?
Anyway, another question. This same function is giving me headaches where it doesn't seem to give the correct angle. I'm trying to use the engine I'm using's built-in logging function to see what it gives for the values of certain things so that I can try to fix the mathematics. Only thing is, the Log() method (I'm pretty sure you call it a method) doesn't accept number variables. How can I convert the variables to a string, so they can be outputted to a file?
Here's approimately what I'm trying to do, horribly wrongly I know. Wrong as trying to fit a square peg in a round hole, I know. This makes me look like a downs patient, I know. Please don't dwell on that.
Engine->Log( "Given sin: " sin(direction) \n "Given cos:" cos(direction) \n "Given addsin: " addspeed*sin(direction) \n "Given addcos:" addspeed*cos(direction) )
This line is within the function I posted earlier.
Name:
Anonymous2006-06-18 15:20
>>7 but why did the tutorial at http://www.cplusplus.com/doc/tutorial/functions2.html say I needed them?
so far as i can tell, it doesn't actually say you needed them. you may be confused by the code sample void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
but the * in these are part of the *= operator (a nice example of why proper use of whitespace makes for easier to read code)
Name:
Anonymous2006-06-18 15:38 (sage)
a nice example of why proper knowledge of syntax makes it easier to read code
fixt
Name:
Anonymous2006-06-18 15:43
>>7
include the <sstream> header, and use come somewhat like this to convert a number (or any type which works with streams, really) into a string. std::string theString;
std::ostringstream tmpStringStream;
tmpStringStream << SOME_NUMBER_GOES_HERE;
theString = tmpStringStream.str();
First, I initialize a variable to hold the string I want, right? OK, then, what the hell is ostringstream? I read that those double less than signs are an operator that grabs something into a variable, but then, why the hell do I have to append the .str() method to it later?
People are being helpful though, and I appreciate it. I was expecting a ton of downs syndrome jokes because I came to this forum to ask questions relating to programming of all things. (this has usually been the case when going to a programming forum... Dealing with douchers with names like DaRkLoRdThAnAtOsVaDeR or stupid shit like that...)
Name:
Anonymous2006-06-19 0:28
but then, why the hell do I have to append the .str() method to it later?
Stringstream is just a plain buffer that can't be treated like a normal type, but one of the things it can do is turn its contents into a string type: hence the str() method. If you tried to assign the buffer itself to the string your program wouldn't compile.
So, if it's a buffer, does that mean that I can put multiple things in it? Can I like, write (in downs syndrome code... for demonstration, not compilation):
ostringstream buffer;
for (something;something;something) {
if (function_that_returns_1_if_successful(something) != 1) {
buffer << failure information \n << endl;
}
write_to_file(buffer.str()); //I told you it's downs code, I know this isn't how you write stuff to a file.
And get a list of everything that went wrong with a certain function?
What I'm trying to say, is, do the values entered into it stack up? Or is my understanding of what a buffer is off?
I can see how it could be used other ways too, not just for error stuff if it works like that.
Name:
Anonymous2006-06-19 4:23
>>15
There must be something seriously wrong with my compiler. Check out this CRAAAAAZY error message.
Also the fact that ostringstream won't work without std:: at the beginning makes me think that my compiler (Microsoft Visual C++ 6.0) is a pos.
Name:
Anonymous2006-06-19 4:26
So, if it's a buffer, does that mean that I can put multiple things in it?
Yep. Though, be careful not to abuse stringstream. I fail to see why if function foo fails you can't write to the file directly. ofstream fout("somefile");
if ( foo(args) )
fout << "Wow that was awesome! You fail\n";
// etc.
Name:
Anonymous2006-06-19 4:28
This is >>18. MSVC++ 6.0 is a terrible compiler. Use something else to compile your code.
Name:
Anonymous2006-06-19 8:34
>>19
I suspected as much, what with all the things it rejects.
Name:
Anonymous2006-06-19 15:07
>>20
Hey, what's /prog's opinion on MSVC++2005? (Or 8.0)
Are you using multiple namespaces, and if not, are you putting a
using namespace std; after your includes?
Name:
Anonymous2006-06-19 18:02
No, I'm not using multiple namespaces.
Will that fix its inability to comprehend endl?
Name:
Anonymous2006-06-19 18:11
How about not using endl? Type <<"\n" instead.
Name:
Anonymous2006-06-19 19:43
>>17
ostringstream and endl are both in namespace std. this is normal behaviour in pretty much any C++ compiler you're likely to use. you want to either tack a "using namespace std;" somewhere so the compiler knows which namespace to look in, or explicitly call std::ostringstream and std::endl each time you use them.
in other words, MSVC++6.0 may suck, but not for this reason.
Name:
Anonymous2006-06-20 8:26
>>26
Yeah, I figured that out on my own. Found out this weird feature where (apparently) typing a namespace name followed by :: give a list of... (I don't know what they're called... identifiers?) Anyway, endl was in there. and \n does sound better, so I'll use it. I can't use namespace std though, because the engine I'm using (This is for a platform game) has it's own namespace, and the trouble of typing out std:: in front of all std stuff is better than typing out the namespace for its identifiers.
So, what reason DOES MSVC++6 suck? Because right now, my project won't compile in anything else, and it's going to get complicated to make the damn thing compile in VC++2005 for example. I want to see if it's worth it to change.
Also, for some reason, VC6 doesn't understand null or false either, (gives undeclared indentifier errors on compile) though it does understand true. Can this be chalked up to namespace? I thought false and null were handled by the preprocessor (as in just replaced with 0's and 1's or whatever before even being compiled).
Name:
Anonymous2006-06-20 11:42
>>27
Literally speaking, *everything* gets replaced with 0's and 1's when you work with computers, but that's besides the point. MSVC++ giving you "undeclared identifier" errors is its way of telling you "??? Dude, WTF *is* this shit?" It doesn't know what "null" or "endl" or whatever means unless you tell it beforehand what it means. That is the whole point of #include and namespaces.
Namespaces, for the most part, are not mutually exclusive. If two namespaces do not overlap (do not both define the same thing in different ways), you can use both with no problems. Even if they DO overlap, you will not get errors unless you use a overlapping identifier without telling the compiler which version to use. The reason your program compiles on MSVC++6 and nothing else might be because it's adding or assuming a "using namespace std;" without telling you its doing so.
Name:
Anonymous2006-06-20 23:17
using std::cout;
using std::endl;for great justice
Name:
Anonymous2006-06-20 23:19
How about not using endl? Type <<"\n" instead.
endl flushes buffered streams. "\n" doesn't.
Name:
Anonymous2006-06-20 23:36
>>30
You mean, it deletes everything else in the buffer? Or puts some sort of logical divide that \n doesn't represent?
Name:
Anonymous2006-06-21 1:15
>>30 I do not think a flush is necessary in this case. >>31 Yes endl empties the stream it was called on, but it's unecessary to flush constantly... one exceptable place is in a loop.
Name:
Anonymous2006-06-21 13:49
>>28
Y'know, this "computers reduce everything to 0's and 1's" concept is true, but there is a higher abstraction immediately above it that is more meaningful and that is true in almost the same number of instances: "computers reduce everything to lists of numbers between 0 and 255" <- saying that is more helpful in getting novices up to speed as far as what happens inside the machine.
>>35
because references are references and pointers are pointers.
Name:
Anonymous2006-06-21 23:57
>>36
You mean, oh. Right. You're just being a troll while contributing nothing new. I know, you don't have to point to references but you do have to point to pointers. Got it.
Name:
Anonymous2006-06-22 4:10
>>37
You'll see it easier this way:
A pointer is a regular new variable whose value is the address of another variable, so to access this other variable you have to use the contents-of operator (AKA dereference, but I like contents-of better, it's more clear).
A reference is an alias to the same memory space, therefore there's no pointer and you do not need the contents-of operator.
>>39
Yes, a reference to a pointer is an alias to the same memory space... of this pointer. Therefore when you use a reference to a pointer you don't use the contents-of operator, but you're refering to a pointer, so if you want to access its contents you need another contents-of operator, but this is not different from using the pointer itself.
I wouldn't mix references and pointers though, it's messy enough the way it is.
Name:
Anonymous2009-01-14 4:27
What a mess of display functions. C++ fails.
Name:
Anonymous2009-03-06 12:54
Them displayed one at least as good as Sussman and The Abelson gathered up a number of problems by installing custom error handlers in the oven immediately 2?
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy