|
FML. I have a map where keys are sorted with std::less<string> to alphabetical order. NOW I realized this motherfucking exercise wants them in ASCII order. So instead of: code:
code:
Is there some automatic way to sort it or must I do some manual fuckery? Also why this stupid rear end project requires C++11 in compilation. I can't use contains() with map so my code is full of functions like code:
|
# ¿ Oct 19, 2022 23:59 |
|
|
# ¿ May 21, 2024 04:40 |
|
Thanks. This is my first c++ course and I really don't have that much of an idea what I'm actually doing The previous one was in Python.. Even the data structure used was like "do whatever you want" so I just semi randomly figured out something which seems to work. Ihmemies fucked around with this message at 07:25 on Oct 20, 2022 |
# ¿ Oct 20, 2022 07:22 |
|
gently caress this C++ course has loving pointers. Of course. I'm supposed to figure out the largest int of an array, and I only have access to the start and end pointer of the array. And I really don't understand anything. I barely managed to do a previous task where I had at least the size of the array provided. Now I don't have even that anymore. Just 2 pointers. WTF, what kind of sadist invented this poo poo
|
# ¿ Nov 15, 2022 19:47 |
|
I just wanted to vent, and I did knot know about any other thread. I have roughly 2 hours left to figure this out (it won't happen). I'll probably have to a book about C, maybe it explains this in a way I can understand Ihmemies fucked around with this message at 19:57 on Nov 15, 2022 |
# ¿ Nov 15, 2022 19:54 |
|
I understand. Maybe I can catch those segfaults when iterating through the array... so the program won't crash from segfault, instead it returns the largest value I've found so far. Edit: can't feasibly catch segfaults, drat. Ihmemies fucked around with this message at 20:09 on Nov 15, 2022 |
# ¿ Nov 15, 2022 20:06 |
|
It's supposed to print something like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 6 9 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 6 9 but I manage to get 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 6 9 9 0 -1887031104 32521 6 0 6 16 1240251648 592132058 -1889350432 32521 -1887035560 32521 -1887035768 32521 -1887034912 32521 -1887035200 32521 -1887915361 32521 -1887035560 32521 -1888119286 32521 -1887035192 32521 -1887035768 32521 -1887037120 32521 So it's not going too hot yet. code:
|
# ¿ Nov 15, 2022 20:33 |
|
Well Actually I still have no idea but this seems to work better, while I was randomly trying to add more special characters to the mayhem:code:
|
# ¿ Nov 15, 2022 20:55 |
|
cheetah7071 posted:yeah that would work, but once the test is over I would absolutely review what & and * actually do I'd rather write python at this point. Luckily there's only 2 weeks left in this most horrible course.
|
# ¿ Nov 15, 2022 21:01 |
|
Don’t worry. Next task is to implement a Book class with 16 mandatory methods, using pointers and recursion. The only choice is that I can use smart pointers instead of regular pointers if I want to. I have one week.
|
# ¿ Nov 15, 2022 21:43 |
|
Xarn posted:When I was teaching programming, I would spend lot of time telling students to read their code as they wrote it, rather than what they think they wrote, or what they wish it did. If you actually read the code, the issue is obvious. Multiple people have said this so far but I still have no idea. I never spot my own mistakes and rarely learn from them so most likely this one will go into the same ever growing pile of mistakes not learned from.
|
# ¿ Nov 16, 2022 10:26 |
|
Well the solution was to not use anything. Just remove everything Thanks, and sorry for being so bad. Anyways. I'm still battling an eternal struggle against on how to check for keys in a map. Is this legal?
|
# ¿ Nov 16, 2022 16:03 |
|
Dijkstracula posted:Coming from Python, you'll find exceptions in C++ are intended for more "exceptional" cases. OK, I'll just do it with find() then.
|
# ¿ Nov 16, 2022 16:23 |
|
Dijkstracula posted:right, OP shouldn't feel that using `if (m.find(k) != m.end()) {...}` is a hack or an antipattern here. I know. I was just curious. This is I think how it should be done: code:
|
# ¿ Nov 16, 2022 17:19 |
|
Sweeper posted:If you are new to cpp bookmark https://www.cppreference.com or add a quick search so you can type “cpp std::map” into url bar thing to lookup docs. I look things up all the time because c++ is inconsistent and many apis work in ways which make sense in the context of the standard, but aren’t intuitive. It’s a great site, bless the maintainers. Yes. Our course even had some homework regarding using CPPref. I just forget to do this often enough. Most of the Book was already presented ready for us. File reading, commandline handling etc. Only the implementation of Book classes methods was left for us. They gave us a ready struct we need to use for the chapters. I managed to do this not quite yet finished or even tested implementation yesterday: https://pastebin.com/uPDac0fA At least some kind of recursion and shared pointers feel a lot easier to use when you can do whatever by yourself, and are not constrained too much by the strict limits set by coursework.
|
# ¿ Nov 17, 2022 14:41 |
|
roomforthetuna posted:Is that Params monstrosity part of what they give you? Jesus. Yes. Function parameters are given inside that params variable. The Chapter struct was given to us. Initially they were regular pointers like this: code:
code:
code:
code:
Xarn posted:An N-ary tree with cyclic references, what could possibly go wrong. I am glad I have no idea, because I don't know what is an N-ary tree, or what are cycling references, or even what can possibly go wrong Ihmemies fucked around with this message at 21:22 on Nov 17, 2022 |
# ¿ Nov 17, 2022 21:15 |
|
Thanks! One reason also was (now when I'm re-reading this week's material) that they said "On this course, we will only get to know the type shared_ptr.". So it should be good enough for now. I guess I'd have to manuallydo a bunch of deleting in class destructor and maybe methods if I used regular pointers instead, and analyze with Valgrind if I succeed in that task or not.
|
# ¿ Nov 17, 2022 21:44 |
|
OddObserver posted:Of more immediate significance: if your shared_ptr's form a cycle, they'll never get cleaned up. So if the program quits they stay in memory? Or those things stay in memory until the program closes? Is it bad if they stay in memory while the program is running? I quickly read about trees and this page suggested to do first child/next sibling data structure: https://www.geeksforgeeks.org/generic-treesn-array-trees/ I guess that is impossible at this situation but from looking at it it looks more efficient? Maybe I should replace the smart pointers with regular pointers and uhh do some manual deletion... Or maybe uhhhh make the uhh parent as a regular pointer and delete those in class destructor? Ihmemies fucked around with this message at 21:54 on Nov 17, 2022 |
# ¿ Nov 17, 2022 21:48 |
|
Well all the the chapter data is needed as long as the created book class object? instance? whatever it's called is alive, since the data is read only once from file. So I guess it's ok that the shared pointers keep existing in memory, as long as the instance is alive and needed. In class destructor, do I need to iterate through the all the chapters and manually delete the pointers, or change parentchapter type to weak pointer or something? Seems they explained the cycles (I looked) but I forgot already about it. My memory is very bad in general. Or should I change the parentchapters to unique pointers or something.. hmm.. I need to study more.
|
# ¿ Nov 17, 2022 22:06 |
|
cheetah7071 posted:You almost never need to manually write a destructor. The default one covers you 99% of the time. The biggest exceptions in aware of are if you need to do something other than destroy objects for some reason, or if you're interfacing with C code and thus working with objects that don't have destructors But in this case I need to clearly do something else than I'm doing, because of that circular shared ptr thing. I just need to figure out what would work better.
|
# ¿ Nov 17, 2022 22:10 |
|
Course said don't mix regular and smart pointers. So I won't be mixing them. Since I don't understand at all the differences between smart pointers, and I understand even less about how to manually release memory of regular pointers when they are not needed anymore, I'll just keep using the shared pointers. The user can buy more RAM if he runs out of it because of shared pointer linking. I don't give a drat.
|
# ¿ Nov 18, 2022 10:53 |
|
Sorry. This will use shared pointers for everything. I don't have the time or capacity to learn to do it any other way at this stage.
|
# ¿ Nov 18, 2022 13:15 |
|
Ideally I'd learn more about different types of smart pointers if I had the possibility to do a project from the ground up. Now I don't understand enough about my program to refactor it again to work with different kind of pointers. I mean I got it to work "perfectly" as in the code gives expected printouts and it passes the tests. Now the data sits in memory until program is closed, because of the linked shared pointers. Anyways, uhh, maybe I could at least find a way to lessen the repetition. It has so many methods which have nearly the same contents, just copy pasted to other methods. Like printLongestInHierarchy vs printShortestInHierarchy, printParentsN, printSubchaptersN etc. I have to figure some way to remove repetitive code, since it's penalized in grading...
|
# ¿ Nov 18, 2022 15:01 |
|
This week's C++ task said posted:Many of the functions do similar things. You should think carefully, which utility functions to implement in order to avoid repeating code. So. We have stuff like:
And I have been at the task, combining 2 methods to 1, avoiding repeating code, with ternary operators! C++ code:
C++ code:
C++ code:
I have this like 7 times in different methods, still researching a way to combine them too. Maybe all the methods should call an utility method, which does this, and then that utility method calls the correct method? C++ code:
E: nvm the comments in files forbid us from editing them: Student's don't touch this file. This week's C++ task said posted:You need not modify the ready-made functions. It is not necessary to understand the functionality of the modules Utils or Cli. However, since the topic of the round is modularity, this is a good example to be explored as a whole. Ihmemies fucked around with this message at 20:56 on Nov 18, 2022 |
# ¿ Nov 18, 2022 20:49 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
I have no idea at all what MDI is. I'm just trying to get this done. I don't think I've ever played a game where options are visible on the same screen as the actual game, they've always been in a separate options menu or something. And preferrably while playing, the option menu is hidden until ESC/F10 is pressed.. I don’t want the game view to be scrollable either. A snake game is nota hex strat game with a big scrollable map either. Well. Maybe I’ll eventually figure out a way to tell the computer what I want done. Ihmemies fucked around with this message at 08:23 on Nov 29, 2022 |
# ¿ Nov 29, 2022 08:18 |
|
Well I finally figured out what was missing. The graphics item class constructor was empty, while it actually needed this to the constructor:code:
Or maybe it's intended. At least I'm getting some results now finally!
|
# ¿ Nov 29, 2022 12:52 |
|
|
# ¿ May 21, 2024 04:40 |
|
drat python is so slow. I made this AoC day 8 puzzle in python first, then cpp. Python was 90ms, cpp 7ms. I tried to implement them the same way.
|
# ¿ Dec 8, 2022 23:17 |