|
Wow, they sure did a number on C since I was a kid. Then again, C++ looks very different from what I got in undergrad, too.
|
# ? Nov 20, 2022 02:01 |
|
|
# ? Jun 3, 2024 12:31 |
|
cheetah7071 posted:while I am still deeply curious how I managed to get a situation where the linker was complaining about being unable to find a file I wasn't trying to link to, I switched to vcpkg and everything works now so good riddance, I guess Boost will use #pragma comment(lib, ...) when the build system is MSVC (or Borland!) This means that the boost headers that you use will auto-link the appropriate boost library. If your build system doesn't install the libs with the correct file name, or you don't pass in #defines to control the auto-linking, you'll have problems. See also: https://www.boost.org/doc/libs/1_63_0/boost/config/auto_link.hpp Nalin fucked around with this message at 22:28 on Nov 20, 2022 |
# ? Nov 20, 2022 10:16 |
|
oh god, I forgot about that misfeature
|
# ? Nov 20, 2022 11:32 |
|
So, I got feedback about a project and it said I need to initialize class variables. I was like ????? I searched for a reason and apparently it is this: If I have a map like this in class: C++ code:
C++ code:
C++ code:
|
# ? Nov 22, 2022 16:58 |
|
Ihmemies posted:So, I got feedback about a project and it said I need to initialize class variables. I was like ????? Use initializer lists in your constructors, like this: https://en.cppreference.com/w/cpp/language/constructor
|
# ? Nov 22, 2022 17:03 |
|
Twerk from Home posted:Use initializer lists in your constructors, like this: https://en.cppreference.com/w/cpp/language/constructor Well I have only one constructor and there's nothing in it. I just want to avoid that "intialize your class variables, minus 5 points" message I will get. I don't want to do anything complicated. I don't understand really a word about what that page says That one class variable I use must be initialized properly, I just have to figure out how to do it. I don't want to add any data to the variable (map). I just want that the map exists, and is ready if someone ever wants to add data to it later.
|
# ? Nov 22, 2022 17:19 |
|
Ihmemies posted:So, I got feedback about a project and it said I need to initialize class variables. I was like ????? Using an uninitialised variable is not what I would consider a type safety issue. As for your question, the default constructor of a class should normally(*) create valid, fully initialised objects. This applies to many (most?) of the container classes in the standard library. For a map you don’t need the initialiser (but it probably doesn’t hurt to write it). * it’s C++, so of course this is not always the case. For example, std::array<T,N> requires an initialiser, unless the type T itself guarantees that its default constructor creates fully initialised objects
|
# ? Nov 22, 2022 17:22 |
|
Zopotantor posted:Using an uninitialised variable is not what I would consider a type safety issue. Uninitialized bools say hi, and worm their way into to your nightmares. Then the rest of undefined behavior pops in for a chat... But yeah, in real world, as was stated above, the map doesn't need an initializer and ints/bools/doubles, etc., do; in your class? Ask your TA/professor/teacher as appropriate.
|
# ? Nov 22, 2022 17:35 |
|
Zopotantor posted:Using an uninitialised variable is not what I would consider a type safety issue. I think I would. A type system guarantees that, if you play by the rules, all values have the properties of their specified type. In the case of `bool`, that means "can only have values 1 or 0 when converted to int" and "can be exhaustively tested by comparing to `true` and `false`". If the type system holds, this can't assert: code:
|
# ? Nov 22, 2022 18:27 |
|
For proper classes, instead of built-in types, they'll be constructed using the default constructor if you don't specify otherwise. Either they just wanted to to clarify that you were using the default constructor as like, a coding clarity thing, or they were talking about a member other than that map.
|
# ? Nov 22, 2022 18:52 |
|
Subjunctive posted:I think I would. A type system guarantees that, if you play by the rules, all values have the properties of their specified type. In the case of `bool`, that means "can only have values 1 or 0 when converted to int" and "can be exhaustively tested by comparing to `true` and `false`". If the type system holds, this can't assert: OK, that makes sense. Enums would have the same problem.
|
# ? Nov 22, 2022 19:43 |
|
cheetah7071 posted:For proper classes, instead of built-in types, they'll be constructed using the default constructor if you don't specify otherwise. Either they just wanted to to clarify that you were using the default constructor as like, a coding clarity thing, or they were talking about a member other than that map. I had 2 uninitialized maps in the class header file. No other variables, only methods. They can’t possibly mean anything else.
|
# ? Nov 22, 2022 22:15 |
|
Ihmemies posted:I had 2 uninitialized maps in the class header file. No other variables, only methods. They can’t possibly mean anything else.
|
# ? Nov 23, 2022 00:16 |
|
Finally a friend said what I must do. If I create a vector like vector <string> to_sort; I must do vector <string> to_sort = {}; instead. If I create a map like map<string, shared_ptr<Chapter>> idstore; I have to do map<string, shared_ptr<Chapter>> idstore = {}; instead. So the magic thing I must do is to add this after every vector and map to fix the problems: C++ code:
Edit: and now I maybe finally perhaps understand? I must list instead all the class variables like this in the constructor, separated by , C++ code:
Ihmemies fucked around with this message at 11:32 on Nov 23, 2022 |
# ? Nov 23, 2022 10:29 |
|
do whatever you need to pass the course, but that's poo poo code if I ever saw one
|
# ? Nov 23, 2022 11:44 |
|
Xarn posted:do whatever you need to pass the course, but that's poo poo code if I ever saw one The only issue is that I don’t know what to do. So I just do whatever I can find and understand. There is tons of info I can’t seem to understand by myself, about c++. I think I now understand how that works what I just pasted here, but I don’t know or I don’t understand any other ways to initialise class variables in class constructor. I was told that initializing class variables as empty with = {} in class header is even worse. Please teach me master. Ihmemies fucked around with this message at 12:52 on Nov 23, 2022 |
# ? Nov 23, 2022 12:49 |
|
Ihmemies posted:Finally a friend said what I must do. https://en.cppreference.com/w/cpp/language/default_initialization That should probably default initialize. In general, you want unordered_map instead of map, but it likely doesn't matter a whole lot for your class.
|
# ? Nov 23, 2022 13:56 |
|
leper khan posted:https://en.cppreference.com/w/cpp/language/default_initialization So.. what should I do otherwise in my code? Something must be done otherwise since the way I initialise my class variables is apparently poo poo. Can you give me an understandable example? Thanks 🙏
|
# ? Nov 23, 2022 14:09 |
|
Ihmemies posted:I was told that initializing class variables as empty with = {} in class header is even worse. Also, things that default-initialize to empty don't have to be explicitly initialized at all. Basically, your course is teaching you weird cargo-cult nonsense. When people say e.g. "that's poo poo code if I ever saw one" they're not knocking *your* code, they're talking about how bad the course is to push this bullshit on you. Since that's what you have to do to pass, write the poo poo code, it's not *wrong*, it's just pointlessly ugly because someone made a bad rule for your course.
|
# ? Nov 23, 2022 14:53 |
|
So what I’m doing is not really neccessary. And I am doing this only because for the sake of appearances, thank you.
|
# ? Nov 23, 2022 15:30 |
|
Ihmemies posted:So what I’m doing is not really neccessary. And I am doing this only because for the sake of appearances, thank you. Yeah, to be clear: it's always a good idea to make sure variables are initialized before use. However, the default construction of container classes like std::vector and std::map initializes them to reasonable defaults (an empty container). Presumably your instructor just wants you to make that explicit, which is fine, I guess, it doesn't hurt anything, but it's a little silly.
|
# ? Nov 23, 2022 16:33 |
|
This thread is a nice example of why a lot of people hate C++. I was also taught C++ the wrong way. Endless exercises of "which method overrides which" and dumb multiple inheritance problems. After avoiding it for years I finally got a job where it was asked of me to write in modern C++ and boy, did I realize my teacher was bad. And how loving good C++ can be with its modern additions. His courses basically stopped at C++98.
|
# ? Nov 23, 2022 17:11 |
|
Well I don't really hate c++. It feels like I make a lot less erros with c++ than with python. I've read about new c++ features and I use them when possible. I think it's quite nice language to use. Especially print() coming in c++23 should be great instead of this something + something + something or << something << something << something. It's just that c++ has so much stuff to get code working, that it's hard to remember or understand everything.
|
# ? Nov 23, 2022 17:34 |
|
New print in c++23? What was wrong with printf?
|
# ? Nov 23, 2022 17:50 |
giogadi posted:New print in c++23? What was wrong with printf? Not type safe, not extensible. Yes, lots of compilers have special magic that recognizes format strings and attempts to type check the arguments, but a native C++ implementation with constexpr logic can actually allow a pure implementation that isn't dependent on compiler magic and can also be extended by user code.
|
|
# ? Nov 23, 2022 17:55 |
|
Now I need to somehow learn to appreciate ANSI C. I need to print out numbers 1 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97 in a loop. but the last one must have \n exactly after it, not allowed to print one space after it. My blood pressure is already rising. I guess I need to calculate the next number, and if there's no next number (after 97), I won't print a space after 97..
|
# ? Nov 23, 2022 20:33 |
Don't print the separator (space or newline) after each number. Print it before each number.
|
|
# ? Nov 23, 2022 20:38 |
|
nielsm posted:Don't print the separator (space or newline) after each number. Print it before each number. Just not the first one.
|
# ? Nov 23, 2022 20:41 |
|
Yes. I guess C forces to think. In python could just add it all to string and not print the last char of the string. Not so easy here..
|
# ? Nov 23, 2022 21:10 |
|
You could still do that it'd just be kind of inefficient
|
# ? Nov 23, 2022 21:19 |
|
leper khan posted:Per my previous post, everything but operator overloads, yes. Anyway, if anyone's curious how the story ends (I doubt it), I ran the updater though Cutter disassembler and decompiler which was luckily reasonably readable thanks to being pretty simple and having lots of debug messages. This is the main function, and there's clearly a line that prints what I need (after the last comment) so it was pretty easy to backtrack What I think is the hid_read_data function. Very helpfully, the debug messages will even tell you which API calls failed It was going ok but eventually I got pretty frustrated, maybe I was screwing something up but sometimes it wouldn't let me rename variables and functions, and in any case I realized I could ever only get 60 bytes of data out of it without major edits. Instead after some searching, I found an awesome app called API Monitor which does exactly that. You can see the exact sequence and parameters of all the API calls, which made writing the code from scratch pretty trivial. SetOutputReport tells it what you want, and the details are in the buffer. 0e is the report id, then 80,50 is the address to read, and 04 I guessed to be the bytes to request (correct). The next call updates the buffer with the output, 5 bytes of header and the data. Other than some boilerplate to get things initialized and find the right device, that's pretty much all: C++ code:
|
# ? Nov 24, 2022 00:44 |
|
So, the last subject in our C++ course is quick GUI apps with Qt. A course provided template project won't comile. A fresh Qt widget project won't compile. It just says: code:
Edit. Well Google provided results instead of DGG. RIght after I figured out I better try another search engine. "apt install libgl1-mesa-dev"
|
# ? Nov 24, 2022 20:11 |
|
Asked this somewhere else, was told this might be a better place: any good book to learn C++20? I learned C++11 reading "The C++ Programming Language" when the updated version covering that came out, but I've been away from C++ land for a while now so this is a good opportunity to refresh my memory and see what was added since then. There's no C++20 version of Stroustrup's book though so I need something else. Ideally something that goes into roughly the same amount of details and is as comprehensive as possible for the new stuff.
|
# ? Nov 24, 2022 20:34 |
|
This may be of help: http://cppstd20.com/ I haven't actually read this book, but I've read the author's past works and I find he does a good job of covering things.
|
# ? Nov 25, 2022 01:40 |
|
That's pretty much exactly what I was looking for, thanks.
|
# ? Nov 25, 2022 04:56 |
|
Now I need to make a Snake game with QT Widgets. The game board must consist of squares, so I thought I'd make a bunch of squares with QGraphicsGridLayout and add square item objects to each grid. But, if I try to setLayout() a QGraphicsGridLayout inside a QWidget window, it says: code:
How do you create a QGraphicsWidget window in Qt? I guess I need that to be able to use QGraphicsGridLayout. I just want a bunch of squares on screen, in a window, goddamnit. Do I need to create a qgraphicswidget inside a qwidget so I can add some graphis grids? ? ? This is so complicated. Ihmemies fucked around with this message at 16:17 on Nov 28, 2022 |
# ? Nov 28, 2022 16:13 |
|
Ihmemies posted:Now I need to make a Snake game with QT Widgets. Did you look at the documentation https://doc.qt.io/qt-6/qgraphicsgridlayout.html ? They have an example there on how to use it (add it to a QGraphicsScene ). Does this not work for you?
|
# ? Nov 28, 2022 17:26 |
|
Volguus posted:Did you look at the documentation https://doc.qt.io/qt-6/qgraphicsgridlayout.html ? They have an example there on how to use it (add it to a QGraphicsScene ). Does this not work for you? I have a mainwindow class, which is like a "launcher" for the game. Then I have game window, which uh I guess runs the game logic and tells the graphics what to do. Then the game_board class is actually which holds the graphics data? So mainwindow -> gamewindow -> gameboard. Gamewindow looks like this: code:
code:
|
# ? Nov 28, 2022 21:56 |
|
Well, the first issue that i'm seeing is that the scene is destroyed after the Game_window constructor is finished. Game_window is a QMainWindow, right? Once the scene is created on the heap and you make the view the central widget of the main window, it should work. The parent of the view should be this.
Volguus fucked around with this message at 22:23 on Nov 28, 2022 |
# ? Nov 28, 2022 22:13 |
|
|
# ? Jun 3, 2024 12:31 |
|
Volguus posted:Well, the first issue that i'm seeing is that the scene is destroyed after the Game_window constructor is finished. Game_window is a QMainWindow, right? Once the scene is created on the heap and you make the view the central widget of the main window, it should work. The parent of the view should be this. Well, no. I guess I can have many QMainWindows? Like, the launcher where user can define the game area size, like x,y, set seed etc. is a QMainWindow. Then when user has figured out what kind of game he wants to play, it launches the game_window. Anyways I made view and scene class variables, and changed the game_window from qwidget to qmainwindow. Then I can use setcentralwidget and it puts something to the screen, finally: code:
I mean it displays a tiny window with nothing, but I can close it and move around. Not ideal, yet: Ihmemies fucked around with this message at 22:58 on Nov 28, 2022 |
# ? Nov 28, 2022 22:55 |