|
If all you are doing is printing out the addresses to look at them you can also just use %p in printf-family functions to print pointers in hex.
|
# ? Apr 2, 2014 15:50 |
|
|
# ? Jun 7, 2024 21:15 |
Unfortunately %p will print out the hex value and I just really wanted to compare also the int value as well
|
|
# ? Apr 2, 2014 18:30 |
Sulla-Marius 88 posted:Unfortunately %p will print out the hex value and I just really wanted to compare also the int value as well Keep in mind, it's the same thing. Same numeric value, different ways of writing it. e: Bunch of stuff about number systems etc nielsm fucked around with this message at 19:11 on Apr 2, 2014 |
|
# ? Apr 2, 2014 19:06 |
|
I am probably going to post a lot of stupid questions over the next few days. I have been asked to supply a C++ program that performs a certain task, as an evaluation exercise for a company I sent a job application to. This company does all Windows stuff, and they've indicated that they will be compiling the code I supply using MS Visual Studio. So, I figured it would be a good idea to do the coding in one of the free editions of Visual Studio. Previously I've done some stuff with C++ using Code Blocks. But it's the same language right, should just be a different interface and not a big deal? Errm... Well, anyway, I want to start by testing that I can parrot back a command line argument. This is what's staring me in the face from the main .cpp file that was automatically generated for me by the IDE. I added the #include <iostream> and all the lines inside _tmain() (except the return 0) myself. code:
code:
code:
I miss Python already. edit: I found some StackOverflow questions that suggested that this was something to do with UTF-16 strings and that I should use std::wcout instead. That doesn't fix the problem. Hammerite fucked around with this message at 20:06 on Apr 4, 2014 |
# ? Apr 4, 2014 20:00 |
|
You have UNICODE defined. That makes <tchar.h> define _tmain and TCHAR as wmain and wchar_t. std::cout doesn't know how to output a wide string (aka wchar_t*), but it knows that it's some kind of pointer, so it'll print the address. printf's %s format specifier also expects a narrow string (aka char*). So when it gets a wide string as its parameter (which is UTF16LE on Windows), it'll output a byte from the string (which will be the least significant byte of the first wide character), and then, since you pass in UTF-16 encoded characters from the ASCII range, the next byte will be a 0x00, at which point printf stops. If you want to print wide strings with printf, you need to use %ls instead of %s. Alternatively, if you want to use iostreams, use std::wcout and drop the asterisk from your argv access.
|
# ? Apr 4, 2014 20:32 |
The reason for behavior you're seeing is the Microsoft way of writing a program that theoretically can compile to both a "Unicode" (wide-string) and an "ANSI" (multibyte-string) version, this is the _t _T prefixed stuff. By default, newer versions of Visual Studio only generate a project configuration for Unicode so despite the code form, you're only ever getting the widestring versions of things. In other words, _tmain() becomes wmain() and _TCHAR becomes wchar_t, and more. But you're then passing a widestring into something that expects an mbcs string instead, so it reads the string byte by byte and since the second byte of low ASCII text in UTF16 LE is always a nul, it stops there. I would suggest dropping the TCHAR stuff and just be explicit about character widths everywhere. The only use for non-widestring software on Windows is if you need to run on Windows 98 (or Me or 95), and you most certainly don't. On the other hand, for a simple console application you may not need to handle Unicode stuff, unless you need to open files by user-supplied filename. So write your main function for mbcs strings instead: C++ code:
|
|
# ? Apr 4, 2014 20:33 |
|
Thankyou both, this is really helpful. This isn't an application that will need to handle text strings as a part of its main functionality, so dropping the _t stuff as you suggested seems like a sensible way to go. Hammerite fucked around with this message at 21:00 on Apr 4, 2014 |
# ? Apr 4, 2014 20:53 |
|
Bonfire Lit posted:printf's %s format specifier also expects a narrow string (aka char*). So when it gets a wide string as its parameter (which is UTF16LE on Windows), it'll output a byte from the string (which will be the least significant byte of the first wide character), and then, since you pass in UTF-16 encoded characters from the ASCII range, the next byte will be a 0x00, at which point printf stops. If you want to print wide strings with printf, you need to use %ls instead of %s. Alternatively, if you want to use iostreams, use std::wcout and drop the asterisk from your argv access. This is such a common bug, it's too bad MSVC doesn't warn for it.
|
# ? Apr 4, 2014 21:18 |
I'm getting a weird error when trying to initialise a class as a member of another class. I have a Character class with a member Rect bounding, but for some reason when I call the Rect constructor in the Character constructor it, for some reason, thinks I'm not giving it any arguments. I made a dummy class to see if I could recreate the issue, and it seems to be a general problem with Rect. This is my Dummy class: C++ code:
C++ code:
C++ code:
code:
If I make bounding into a pointer to a Rect and allocate it on the heap with new, the class compiles and runs as expected, but I'd rather avoid allocating on the heap, since I'm working on an embedded system and every bit of optimisation helps. This is the Dummy class that works btw: C++ code:
Joda fucked around with this message at 14:34 on Apr 7, 2014 |
|
# ? Apr 7, 2014 13:55 |
|
C++ code:
This is ok though, because you don't need a default constructor. You can just have the Dummy constructor call your user-defined Rect constructor instead, using an initialization list: C++ code:
|
# ? Apr 7, 2014 15:00 |
|
Also, you don't need to use "this->" to work with data members of the class the method belongs to. The compiler assumes you're referring to "this" object
|
# ? Apr 7, 2014 15:10 |
code:
I also tried adding an empty default constructor for Rect and now my code compiles fine. However, when I make a call for bounding from main() I get the following error: code:
C++ code:
I'm coming from Java, so I'm sure I picked up loads of bad habbits which are giving me hell now.
|
|
# ? Apr 7, 2014 15:31 |
Joda posted:
Yeah, Dicky B made a typo in his example. Try this instead: C++ code:
But as the error message you got also hints, the same syntax is used to call the constructor of superclasses from subclasses, that's done by using the name of the superclass for the initializer.
|
|
# ? Apr 7, 2014 15:40 |
Sweet! That fixed it. Thanks for the help. I'm still a bit confused as to why I can initialise an iVec2 in a constructor and not a Rect, but at least now I know how to handle that error.
|
|
# ? Apr 7, 2014 15:59 |
|
Joda posted:Sweet! That fixed it. Thanks for the help. This is the key information you need from Dicky B. Dicky B posted:The compiler will generate a default constructor for you only if you don't explicitly define any constructors yourself. Since you already defined a constructor for Rect, the compiler did not generate a default no-argument constructor for Rect.
|
# ? Apr 7, 2014 16:26 |
I guess I didn't explain this, but iVec2 has a specified constructor as well, and no default constructor.
|
|
# ? Apr 7, 2014 16:32 |
|
You're confusing initialization with assignment.C++ code:
C++ code:
|
# ? Apr 7, 2014 16:34 |
|
Here is a new stupid question to delight everybody. I want to test that I can write to a file, and read from it. Turns out I can do the first one of those, but not the other. Here's my code: code:
code:
As far as I can tell I'm doing what it says in this tutorial, so I don't know why it doesn't work for me. edit: obviously it doesn't spit HTML character entities back at me, that's the forums doing that.
|
# ? Apr 8, 2014 00:50 |
|
code:
|
# ? Apr 8, 2014 00:53 |
|
nvm;dumb
WaterIsPoison fucked around with this message at 01:11 on Apr 8, 2014 |
# ? Apr 8, 2014 00:58 |
|
pseudorandom name posted:
I see that std::cout << current_line << "\r\n"; works in place of the printf line. I should have thought of trying that, but I didn't. WaterIsPoison posted:Hammerite, is this part of your coding interview? It is something that I needed to work out in order to tackle the evaluation exercise they sent me, yes.
|
# ? Apr 8, 2014 01:10 |
|
For my current CS project we have to write a pseudo Ipod. We have to hardcode "songs" that we would like to store in memory and print out a list of these stored songs to the terminal. You can have a maximum of 25 songs or a 100MB of songs total, whichever comes first. It's basically a lesson in memory management. Here's a question, previously I wrote this program using an array for the song list and everything worked fine. This time around we're not allowed to use an array to store the songs and manage memory. We have to store the "songs" in a binary .dat file and work from there. How in the gently caress would I even do this? We also have to create two separate classes one class for adding, removing, shuffling songs..ect ect and another class for the actual "song(s)". I'm having trouble wrapping my head around if I'm supposed to hardcode 25 different song objects so I can work with 25 potential "songs" or am I missing something here? a "song" is just (song title, artist name, size) in text (or binary in this case). we're not actually loading in real songs.
|
# ? Apr 8, 2014 01:35 |
|
http://www.cplusplus.com/doc/tutorial/files/
|
# ? Apr 8, 2014 01:42 |
|
So for some reason my client code isn't recognising Class Alumni as a type. ColMbrs.h: C++ code:
C++ code:
C++ code:
C++ code:
Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice.
|
# ? Apr 8, 2014 02:07 |
|
Tusen Takk posted:So for some reason my client code isn't recognising Class Alumni as a type. Class isn't a keyword. Tusen Takk posted:Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice. Nope.
|
# ? Apr 8, 2014 02:13 |
|
pseudorandom name posted:Class isn't a keyword. haha boy I am an idiot edit: it still is saying "Unknown type name 'Alumni' though edit 2: well gently caress me I never #include "Alumni.h" FAT32 SHAMER fucked around with this message at 02:18 on Apr 8, 2014 |
# ? Apr 8, 2014 02:16 |
|
Tusen Takk posted:Anyone have any idea why it wouldn't be recognising Alumni but it recognises Student? Because you're including Student.h and not Alumni.h (Alumnus, ahem). Unsigned ints are often the right thing; array indices for example tend to fit them. What did you think was wrong with them?
|
# ? Apr 8, 2014 02:17 |
|
Tusen Takk posted:Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice. Star War Sex Parrot fucked around with this message at 02:22 on Apr 8, 2014 |
# ? Apr 8, 2014 02:18 |
|
Subjunctive posted:Because you're including Student.h and not Alumni.h (Alumnus, ahem). Yeah, I just realised that. In EvE Online the turbonerds are always "lol unsigned ints" when you have a stack of object x greater than whatever the largest number an unsigned int can accept or something like that. There are other examples in other games but I can't think of any other examples past that. After adding the #include "Alumni.h", now it is giving me an error saying "Assigning to 'ColMbrs *' from incompatible type 'Alumni *'. I'm guessing I don't have pointers somewhere that they need to be
|
# ? Apr 8, 2014 02:23 |
|
The only problem I could think of is that there's a potential for many unexpected type conversions, I've seen people avoid them for being unnecessary with a few caveats but I'll stick with them as long as I get compiler warnings for comparing an int to the size of a vector or the aforementioned indices. That and people who just use unsigned instead of unsigned int, but that's just a personal preference and they are completely equal.
|
# ? Apr 8, 2014 02:23 |
|
Tusen Takk posted:In EvE Online the turbonerds are always "lol unsigned ints" when you have a stack of object x greater than whatever the largest number an unsigned int can accept or something like that.
|
# ? Apr 8, 2014 02:24 |
|
A stack of things bigger than what you can fit in an unsigned int would be pretty impressive.
|
# ? Apr 8, 2014 02:39 |
|
Star War Sex Parrot posted:Using a signed int wouldn't help you with this. So basically the turbonerds are elitist idiots on top of being nerds. Got it. My only errors now are for Alumni.h. on the constructor for Alumni it is giving me errors "no matching constructor for initialisation of 'ColMbrs'" and "expected expression".
|
# ? Apr 8, 2014 02:40 |
|
Tusen Takk posted:Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice. Yes, unsigned int is redundant, you should use unsigned instead.
|
# ? Apr 8, 2014 02:48 |
|
The problem with the Eve Online inventory code is that it uses signed integers to store item quantities. Given that there's no way to have negative amounts of an item in your inventory there's no point using a signed value to store that information. This halves the maximum quantity that can be represented in a single stack of items (from ~4 Edit: I can't count ephphatha fucked around with this message at 06:23 on Apr 8, 2014 |
# ? Apr 8, 2014 03:02 |
|
Unsigned ints also have the benefit of being guaranteed by the standard to obey modular arithmetic, so you don't get UB on an overflow. Billion, not million tractor fanatic fucked around with this message at 03:26 on Apr 8, 2014 |
# ? Apr 8, 2014 03:08 |
|
Tusen Takk posted:So basically the turbonerds are elitist idiots on top of being nerds. Got it. Ephphatha posted:The problem with the Eve Online inventory code is that it uses signed integers to store item quantities. Given that there's no way to have negative amounts of an item in your inventory there's no point using a signed value to store that information. This halves the maximum quantity that can be represented in a single stack of items (from ~4 million to ~2 million). Even so the only people who run into the item stack limit are the trillionaires with massive stacks of minerals like mynnna. edit: finished it. haha this loving professor hasn't graded any of my projects since HW 2 and we're on HW 8. He is either hilariously behind or he has no idea who I am. FAT32 SHAMER fucked around with this message at 03:38 on Apr 8, 2014 |
# ? Apr 8, 2014 03:20 |
|
b0lt posted:Yes, unsigned int is redundant, you should use unsigned instead. Or you could just go kill yourself for wanting to use an unsigned int. Unsigned int is the maximal code smell as far as unsignedness goes.
|
# ? Apr 8, 2014 04:45 |
|
Every for loop that I write uses an unsigned short int.
|
# ? Apr 8, 2014 04:53 |
|
|
# ? Jun 7, 2024 21:15 |
|
Star War Sex Parrot posted:Every for loop that I write uses an unsigned short int. (This isn't true at all, but if unsigned short int was a good idea then unsigned char would be a better one for the same reasons. Unless the target platform is a 16-bit machine. In which case a short int probably is a byte anyway and it's all gone horribly wrong please stop me I can't get out of this comment oh god)
|
# ? Apr 8, 2014 05:18 |