When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
138 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
You seem to be using Linux. First, compile with -g -fsanitize=undefined,address, second run it under gdb. IDEs such as VSCode and Qt Creator will run it in a debugger automatically. It allows you to see where it crashed, how it got there and what the variables currently are when the crash happens.
Close enough, should still work.
I think it's xcode as the IDE, but the rest should be the same.
-g is for adding debug symbols, it allows gdb to figure out what the program is doing.
-fsanitize=address,undefined activates sanitizers that will attempt to detect errors and report them. So ideally instead of "segmentation fault" you get "Invalid read access on address xxx in main.cpp:xxx allocated in main.cpp:xxx and freed in main.cpp:xxx". Sometimes it helps, sometimes it doesn't, but it's essentially free.
Looks like that is essentially VSCode. They have instructions at https://code.visualstudio.com/docs/cpp/cpp-debug. They even have sections on macos.
Apparently you're supposed to use lldb instead of gdb on macos.
yeah step 1 is going to be to figure that out, a debugger is the #1 most important tool
Yes, VSC is based on extensions. It should recommend you the most popular one.
You press the little 🪲 symbol or usually F5.
And then the program runs as normal.
And if there is a problem like a segmentation fault the debugger stops the program and shows you the line where it crashed with a call stack to see how it got there and variable contents.
No clue. Ideally don't have one and it uses reasonable defaults.
Whelp, welcome to C++ toolchain pain.
Sometimes it takes days to fix, other times you just give up.
To just get something going, you can try lldb yourprogram and then probably r to run and see what it says.
It's a huge inconvenience compared to a working IDE, but at least you have a debugger.
Alternatively install Qt Creator where everything just works out of the box.
Because that's the bad way to do it.
It means you don't have IDE integration.
You really want your IDE to handle commands to lldb.
That looks like you forgot the -g or something.
It's supposed to show you C++ code, not assembler code, but the debug symbols are needed to figure out which assembler code corresponds to which C++ code.
🤷
Looks like something is borked in your setup.
Just find the magical lines in launch.json and everything works. It's my least favorite feature in VSC.
No, I didn't look at it.
It has funky output on https://onlinegdb.com/7G3dXojYA but doesn't seem to have crashed.
main.cpp:63:9: warning: control reaches end of non-void function [-Wreturn-type]
Should probably fix those: https://coliru.stacked-crooked.com/a/0bcd1ebfe8df519b
The currently logic is "randomly crash, probably". Surely you can do better. An assert(false); would already be an improvement.
Wtf? You should be using one from 1 one
Programming without one is like driving a car blind
Just don't write bugs, then you don't need a debugger 
Never use JavaScript when you can use TypeScript instead.
Because then you're not programming blind.
Yes
Debatable. Unfortunately almost all tutorials and videos about C++ are utter garbage and harm more than help.
I guess then you will not go for the "C++ in 21 days" strategy either. https://abstrusegoose.com/249
Still this code? https://www.onlinegdb.com/7G3dXojYA
You should remove all the class keywords when using a class name. It's just crate, not class crate.
Most people would recommend a book
I don't know. You probably changed something. But this was also a general remark. You seem to add class before every occurrence of crate.
It'd be funnier without the animation.
"std::move doesn't move"
Combine it with storm chair or something.
Storm chair?
haha
@thin oasis Has your question been resolved? If so, run !solved :)
what library is that
STARRED
@thin oasis Has your question been resolved? If so, run !solved :)
Do you have a debugger yet?
See if Qt Creator works.
It's a different IDE.
The difference is that you don't need a launch.json and it integrates compilers and debuggers properly on installation.
Also if you're wondering if it's just you who is too dumb to get the C++ extension to work properly in VSC, it's not. I gave up. Lots of others did too.
No, that doesn't help at all. That's for developing Qt applications which is not what you want.
Yes
🤔
When you do int a[3]; you get an array with 3 elements, the valid indexes are 0, 1 and 2.
That is not allowed in C++.
And no, it would have 0 elements and 0 size and not make sense overall.
Qt Creator should be packaged. I forgot what Macos uses as a package manager, but it should be in there. Alternatively do it the Windows way and download it from https://www.qt.io/download
Yup, top[6] does not exist.
That's good. Liking pain is a requirement for using C++.
true but so is having access to a debugger :)
For the first one you have to decide what happens if someone tries to pop a crate from an empty inventory. Typical ways is to decide that this should not happen and means you did a programming error elsewhere. In that case you use assert(!isEmpty(x));.
Alternatively you say it's a recoverable error, in which you throw std::runtime_error{"Invalid inventory"};.
Alternatively you say nothing should happen, you just grab nothing and you have a special crate value that expresses empty space.
I personally prefer the first 2 approaches
generally returning something empty like this is a bit of an anti pattern in my opinion, sometimes it's desirable but here it often denotes a bug in the program imho
The color error is easier, just give it a value. Instead of class crate newcrate; you do crate newcrate{}; which value-initializes all the members (basically means everything is set to 0).
struct crate newCrate is C syntax
but in C++ typing struct / class before stuff isn't needed anymore
hence the typedef trick often used in C
Normally I'd agree, but given the screenshot it looks like the arm can grab at inventory 7 and grab nothing. That should not be considered an error.
yeah I agree, in this case if it's essentially a crane game grabbing empty space might be valid
though it might be intresting to perhaps return an optional? 🤔
what code is throwing that?
or is it the same as online?
🤔 that should be just fine
Can you post the whole thing again?
omiting them here should be fine but it's weird it's complaining about it
Well, omitting them caused an error with uninitialized variables.
Something is very broken there.
One thing you should do is set a standard.
Add -std=c++20 to your g++ flags.
🤔 no right, with classes it should call the default constructor
Yes, and crate doesn't have an explicit one and the implicit one does nothing.
yeah okay fair enough
I was questioning everything for a second
but yeah since it doesn't set default values it will indeed break
That should just work then.
I don't know.
Blame 🍎
Yes, that's why we're using {} and not ().
I don't know what to tell you. The standard says crate newcrate{}; is fine and it works everywhere else. Either that or I've been tripping the last 10 years.
crate newcrate = {}; is an overcomplicated thing that just happens to also work.
Works just fine: https://coliru.stacked-crooked.com/a/ddfe90776a51e26d
I thought we already went over that. () is wrong, {} is right.
Ah yes the most vexing parse
crate newcrate(); is a function declaration, just like int f();.
By now compilers are smart enough to figure out that you didn't mean to use a function declaration there.
Just for my sanity, this works for you too, right?
Weird. Blame Apple.
They really messed with clang.
It isn't. It clearly says it's "Apple clang".
Which is actually a thing.
This is exactly what I been using as well
Depends on what inventory.push returns.
At a time quick glance it looks like a off by one error
No, i just looked at the animation
The first pickup at position 2 top actually picks up position 3 bottom. And after that everything breaks.
The index error should be easy to fix. That it goes bottom to top requires a little more thinking. I feel like that it grabbed that box is correct, but the display function draws it in the wrong order. The reason being that it leave a hole in the bottom.
If you try to grab from position 7 you should get an index error, at least with the sanitizers, so the location should be easy to find.
no
that seems like your code is broken :D
like your index is at -1 by default or something
or a 1 off error
hmmmmm well an index of -1 wouldn't be a valid index
so a crate being spawned should propably have an index of 0? since there is no 1 crate?
amazing
heyyy
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity