r/cpp_questions Sep 01 '25

META Important: Read Before Posting

170 Upvotes

Hello people,

Please read this sticky post before creating a post. It answers some frequently asked questions and provides helpful tips on learning C++ and asking questions in a way that gives you the best responses.

Frequently Asked Questions

What is the best way to learn C++?

The community recommends you to use this website: https://www.learncpp.com/ and we also have a list of recommended books here.

What is the easiest/fastest way to learn C++?

There are no shortcuts, it will take time and it's not going to be easy. Use https://www.learncpp.com/ and write code, don't just read tutorials.

What IDE should I use?

If you are on Windows, it is very strongly recommended that you install Visual Studio and use that (note: Visual Studio Code is a different program). For other OSes viable options are Clion, KDevelop, QtCreator, and XCode. Setting up Visual Studio Code involves more steps that are not well-suited for beginners, but if you want to use it, follow this post by /u/narase33 . Ultimately you should be using the one you feel the most comfortable with.

What projects should I do?

Whatever comes to your mind. If you have a specific problem at hand, tackle that. Otherwise here are some ideas for inspiration:

  • (Re)Implement some (small) programs you have already used. Linux commands like ls or wc are good examples.
  • (Re)Implement some things from the standard library, for example std::vector, to better learn how they work.
  • If you are interested in games, start with small console based games like Hangman, Wordle, etc., then progress to 2D games (reimplementing old arcade games like Asteroids, Pong, or Tetris is quite nice to do), and eventually 3D. SFML is a helpful library for (game) graphics.
  • Take a look at lists like https://github.com/codecrafters-io/build-your-own-x for inspiration on what to do.
  • Use a website like https://adventofcode.com/ to have a list of problems you can work on.

Formatting Code

Post the code in a formatted way, do not post screenshots. For small amounts of code it is preferred to put it directly in the post, if you have more than Reddit can handle or multiple files, use a website like GitHub or pastebin and then provide us with the link.

You can format code in the following ways:

For inline code like std::vector<int>, simply put backticks (`) around it.

For multiline code, it depends on whether you are using Reddit's Markdown editor or the "Fancypants Editor" from Reddit.

If you are using the markdown editor, you need to indent every code line with 4 spaces (or one tab) and have an empty line between code lines and any actual text you want before or after the code. You can trivially do this indentation by having your code in your favourite editor, selecting everything (CTRL+A), pressing tab once, then selecting everything again, and then copy paste it into Reddit.

Do not use triple backticks for marking codeblocks. While this seems to work on the new Reddit website, it does not work on the superior old.reddit.com platform, which many of the people answering questions here are using. If they can't see your code properly, it introduces unnecessary friction.

If you use the fancypants editor, simply select the codeblock formatting block (might be behind the triple dots menu) and paste your code into there, no indentation needed.

import std;

int main()
{
    std::println("This code will look correct on every platform.");
    return 0;
}

Asking Questions

If you want people to be able to help you, you need to provide them with the information necessary to do so. We do not have magic crystal balls nor can we read your mind.

Please make sure to do the following things:

  • Give your post a meaningful title, i.e. "Problem with nested for loops" instead of "I have a C++ problem".
  • Include a precise description the task you are trying to do/solve ("X doesn't work" does not help us because we don't know what you mean by "work").
  • Include the actual code in question, if possible as a minimal reproducible example if it comes from a larger project.
  • Include the full error message, do not try to shorten it. You most likely lack the experience to judge what context is relevant.

Also take a look at these guidelines on how to ask smart questions.

Other Things/Tips

  • Please use the flair function, you can mark your question as "solved" or "updated".
  • While we are happy to help you with questions that occur while you do your homework, we will not do your homework for you. Read the section above on how to properly ask questions. Homework is not there to punish you, it is there for you to learn something and giving you the solution defeats that entire point and only hurts you in the long run.
  • Don't rely on AI/LLM tools like ChatGPT for learning. They can and will make massive mistakes (especially for C++) and as a beginner you do not have the experience to accurately judge their output.

r/cpp_questions 2h ago

OPEN suggestion needed for cpp projects

4 Upvotes

i currently a 1st year student and i am planning crack google step internship next year so just need some best project suggestion to standout on my resume.


r/cpp_questions 3h ago

OPEN How do I fix enemy generation?

0 Upvotes

https://onecompiler.com/cpp/44qwdefz6

I tried generate enemies depended on chapter but the enemy is still the same (slime). Can you tell me what I did wrong?


r/cpp_questions 22h ago

OPEN Career switch to C++ — how many applications did it take?

28 Upvotes

Hey folks, especially those in the US/Canada who moved into C++ from another software domain.

I have around 7 years of software experience, but for the past 2 years I've been on a career break and using that time to learn and work on C++/Linux/systems programming full-time on my own. Now I'm trying to bring my previous software experience into the C++ world.

Would love to hear simple numbers like:

150 applications → 8 interviews → 1 offer

Also, which C++ domains are easier to break into for someone making this transition — backend/systems, embedded, networking, fintech, etc.?


r/cpp_questions 17h ago

OPEN How to get a C++ Job

11 Upvotes

Its a pretty tough market but i managed to get a job as a backend developer in other languages apart from C++. But I want to grow as a C++ developer as well, how do i gain actual professional experience? Yes i can contribute to github projects but are there any other ways? Appreciate any advice.


r/cpp_questions 15h ago

OPEN Learning cpp for electronics

2 Upvotes

hello everyone so im planning on learning cpp for some "low-level electronics" and embedded systems...

and i've seen multiple people on programming subreddits saying that www.learncpp.com is a good website and tbh after i read some shapters i can say that its indeed a great website,however i feel like that website is just too much for me especially that im not very interested in deep diving into this language and just want to learn what i really need (i already have a short learning deadline)

i took a look at freecodecamp 4h course but i feel like four hours for a language like cpp is too little (i took their python course and it was good tho)

so what im asking for is where can i find some resources that fit my learning goal? ty


r/cpp_questions 1d ago

OPEN VS code or VS community

14 Upvotes

Im a btech first year student who is trying to study c++ independently. I have a lap with 16 gb ram and can perform average tasks. So my question is should I download VS community for coding cpp or is vs code enough. Please keep in mind that i will be learning many other languages too( we are supposed to study c in first year )


r/cpp_questions 1d ago

OPEN What was your first job using C++?

15 Upvotes

I feel like the most difficult part of applying to C++ jobs is they usually require at least 2 years of work experience. What are your recommendations as to jobs or subfields that have a more entry level gate for C++ programmers who need production level experience?


r/cpp_questions 22h ago

OPEN Looking for recommendations for modern C++ embedded platforms and controllers with a forward looking support and development roadmap

1 Upvotes

Looking for recommendations for relatively easy to use/microcontrollers that support C++ emended development out of the box for rapid testing and deployments, purely for iterative prototyping. Intention would be that once a successful prototype is developed, I could utilize the same tooling and microcontroller ecosystem for small deployments (preferably commercial).

Application domain for the microcontrollers would integrated into atmospheric and environmental testing, with the ability to mount additional sensors (photo, video, aquatic, air, and mechanical pulse controllers to controls gimbals and rotational motors) in future experiments.


r/cpp_questions 1d ago

OPEN Including classes and shared_ptr woes

8 Upvotes

It seems that I'm missing something for defining classes in separate files, and creating a shared_ptr to them.

I have one project that gives a series of cryptic messages when I try to do this, so I tried to strip things down to an essential example:

main.cpp:

#include <memory>
#include "thing.h"

using namespace std;



int main()
{ 
  shared_ptr thingPtr = make_shared<thing>();
  thingPtr->Go(); 
}

thing.h:

#ifndef thing_h
#define thing_h

class thing
{
  public:
    void Go();
};

#endif

thing.cpp:

#include <iostream>

class thing
{
  public:

    void Go()
    {
      std::cout << "Yay!" << std::endl;
    }
};

I would expect this to print "Yay!" to the console, but it doesn't even build. Instead, it complains:

[ 33%] Building CXX object CMakeFiles/PTR.dir/src/main.cpp.obj
[ 66%] Linking CXX executable PTR.exe
C:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\PTR.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2c): undefined reference to `thing::Go()'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\PTR.dir\build.make:119: PTR.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:86: CMakeFiles/PTR.dir/all] Error 2
mingw32-make: *** [Makefile:90: all] Error 2

I've searched around, but have been having a surprisingly difficult time finding something that actually explains how to make a class accessible to another class via a shared_ptr and header files. Let me explain that--I have seen tutorials on shared_ptrs, on header files, and on classes. But in almost every case, they explain these concepts in isolation... And the examples they give work--in isolation. For example, forward-declaration of a class works fine... until you use a shared_ptr. Then I get into cryptic posts about things like the PIMPL idiom which seems like overkill for what I'm doing, or other syntactical differences from what I'm doing that I can't tell if they are important or not.


r/cpp_questions 1d ago

OPEN Committing third party include files into your repo

3 Upvotes

This is not work related, I'd like to be able to work on a home project from multiple computers without having to re-download the exact versions of each of the third party tools. These tools are all open source and publicly available, like tensorflow

in my development repo, I would just upload the entire project, libraries and the include files on GitHub, so when I clone it from another computer it just builds without issues.

I know for a publicly released project, it's supposed to link to other GitHub releases and build everything from scratch. So if I were to release it I would remove the third party tools

Is there any issue with actually doing this on GitHub though? Or is there a way to do it correctly if I include an open source license


r/cpp_questions 20h ago

OPEN what is the worst library to integrate in a build (like CMake) ?

0 Upvotes

hello guys

i have a question for you

what's is the worst library to integrate in a build system ?

thanks you to all those who answered my question :)


r/cpp_questions 1d ago

OPEN Why Doesn't This Repo Have .CPP files for its Header Files?

7 Upvotes

Here is the Github repo: [Link](https://github.com/mobizt/FirebaseClient).

I am a C++ noob. I wanted to look at the code in this repo to understand more of what is going on, and all I see are header files. Can someone explain what I am not understanding?


r/cpp_questions 1d ago

OPEN unzip.h is not found with assimp on linux

0 Upvotes

Hello everyone hope you have a lovely day.

so I currently having a problem with building assimp on manjaro linux, everytime it throws this error

fatal error: 'unzip.h' file not found

and if I fixed the path manually it fails by the lld linker to link properly.

how to solve this problem?

thanks appreciate your help!


r/cpp_questions 1d ago

OPEN Dependency injection with ROS

1 Upvotes

I'm trying to better understand dependency injection / mocking a bit better so I can write unit tests that don't require elaborate & fragile setups. Specifically I have a class that verify similar to the ROS2 Minimal Publisher Example. In this example the class creates a timer and a publisher object. The timer just invokes a callback, so I can assume that the callback registration works, and I can set up my test harness to invoke the callback directly. What I would like to do is mock out the actual publish call to ensure timer calls publish without needing to set up a ROS2 subscriber in my unit test.

I'm looking for generic approaches to the specific case (and not necessarily tied to a specific testing or mocking framework)

Here is a stripped down version of the example:

class MinimalPublisher : public rclcpp::Node

{

public:

MinimalPublisher()

: Node("minimal_publisher"), count_(0)

{

publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);

}

private:

void timer_callback()

{

auto message = std_msgs::msg::String();

message.data = "Hello, world! " + std::to_string(count_++);

// *** This is the call I want to mock out ***

publisher_->publish(message);

}

rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;

size_t count_;

};


r/cpp_questions 2d ago

OPEN Is it worth learning another language besides C++?

121 Upvotes

C++ is the only general-purpose language with which I am most familiar, I tried to learn other languages like C# or Rust, but without a clear idea of what to do with those tools, it's more difficult to learn them.

And all the software I use uses or is written in C++, specifically Godot Engine, Blender, LMMS, that's why I always have ideas of what to do with C++.

learncpp.com is amazing and possibly one of the best educational resources for a language in general, either that, or I have Asperger's and they have to explain everything to me in simple terms.

My question is, if this limits me in the long run as a programmer or should I learn an additional language that works well with C++?, I personally wouldn't trust a programmer who claims to know 15 technologies; someone who tries to do too much often accomplishes little, but that's just a personal opinion, what do you think?.


r/cpp_questions 1d ago

OPEN How does C++26's std::hive work for inserting and erasing?

4 Upvotes

I'm trying to make my own (very basic) implementation of std::hive in C and to do that, I of course have to understand exactly how inserting and erasing works.

As I understand it, the index of a given block to insert into can be tracked by its free list, where the free head stores an index and the indices of the actual data itself is repurposed to form a linked list for empty slots

If the free head stores an individual index, and we need to insert into an index that is in the middle of an empty region, how do you figure out the left and right ends of the empty region in O(1) time without looping leftwards and rightwards?

For example, let's take this scenario, where slot 2 marks the start of a 5 slot empty region:

``` Slot index: 0 1 2 3 4 5 6 7 8 9

Skipfield: 0 0 5 X X X 5 0 0 0 ```

I've used X in the middle as these can contain junk values as I understand. Now let's say that the free head stores index 4, and so when we insert into index 4, we need to update the values in the skipfield for slot indexes 2, 3, 5 and 6 to match their new values (2), so that we get:

``` Slot index: 0 1 2 3 4 5 6 7 8 9

Skipfield: 0 0 2 2 0 2 2 0 0 0 ```

But how do we actually figure out in constant time where these boundaries are? If we are only given the index to insert to? Do we somehow store ranges of values instead of individual indicies on the free list?


r/cpp_questions 1d ago

OPEN yet another custom assertion question

2 Upvotes

Sometimes I am writing unit-test inline in my code, then I just use the assert() macro because that compiles out in release builds. And in such cases I don't really need anything in the trace to tell me I have a bug, and I don't need to spin up any unit testing framework. But for easy runtime validation I want a macro I can use but will also print an error message that a user can do something with to realise that they maybe fed in something invalid at a point.

In all cases I really want my macro to terminate the application and thus not memory-scribble or worse. I have been trying to understand the tokenizing operator, and the argument for do {} while () and the side effects of adding (a) around things which obscures types as far as I can tell and still deal with unwanted training ; semicolons that imply that I am not grasping syntax yet.

So I wrote

#include <iostream>
#include <string>
#define assume_true(expression) ((void)(                                        \
        (!!(expression)) ||                                                 \
        (std::cout << #expression << " not true in " << __FILE__ << " LIN: " << __LINE__ << std::endl )) \
    )

And then wanted to make it actually terminate with an std::exit(8) , and it just won't parse unless I replace the || convenience with an if statement.

#define assume_equal(left, right, message) if (left != right) { \
std::cout << #left << " != " << #right << " in FILE: "<< __FILE__ << std::endl; \
    std::cout << message << std::endl; std::exit(8);\
}

uint32_t life = 42;
assume_true(42 == life);
std::cout << "The answer is 42!\n";
assume_equal(42, life, std::to_string(life));
std::cout << "Life is " << 42 << ".\n";
assume_equal(21, life, "LIFE=" << std::to_string(life));

I'm clearly taking a lot of chances here and the way I pass the message in the second macro feels like a total hack, because it is abusing the way the macro preprocessor splits parameters based on commas. I assume that has drawbacks for me later on.

I assume a later toolchain will also help us all when it comes to the side-effect problem of a macro using an argument twice, once for comparison and once for printing. Has anyone just opted to solve this by creating temporaries for things they want to print out in the crash trace? Because that would really mean creating a macro for each temporary type for each parameter I might want to print surely. I get the impression the caller needs to just use a temporary before using the macro, is that what everyone else is doing? Because at that point a function call is just a load less pain surely? I feel I have approached my release-build guard-code completely wrong.

I'm on C++ 17 (yes the rest of my team is on an even older toolchain.) Are release/runtime macros just hard, or am I trying to learn too many things about parsing all at once?

EDIT: Consolidating the answer... As usual thanks so much for the brilliant clues, I now have this little progression * ALWAYS state up front compiler version C++17 * macros really should be UPPERCASE * when printing an error use STDERR not STDOUT

#include <iostream> #include <string> // This macro is terrible, it lacks a scope so I cannot call ;std::exit in it #define ASSUME_TRUE(expression) ((void)( \ (!!(expression)) || \ (std::cerr << #expression << " not true in " << __FILE__ << " LIN: " << __LINE__ << std::endl )) \ )

  • I then moved on to use if {} which allowed me to exit the application

#define ERROR_ASSUMPTION_FAILED 42 #define ASSUME_EQUAL(left, right, message) if (left != right) { \ std::cerr << #left << " != " << #right << " in FILE: "<< __FILE__ << std::endl; \ std::cerr << message << std::endl; std::exit(8);\ }

  • The trouble with that macro is that it really lacks the structure that a lambda might give, thanks to a great suggestion I moved this form, which omits the nice __VA_ARGS__ macro automatic variadic args, which did not expand in C++17, but it's already better

``` constexpr int EXIT_ABORT = 2;

#define ASSERT_EQ(LHS, RHS, MESSAGE ) \
[lhs=LHS, rhs=RHS]() { \
    if ( not (lhs==rhs) ) { \
        std::cerr << "assertion " << lhs << "(" #LHS ") == " \
        << rhs << "(" #RHS ") failed: " << MESSAGE << '\n';std::exit(EXIT_ABORT); \
    } \
}()

```

  • Along the way I realised that this is a GUARD MACRO and the question I had about macro side effects if a macro uses a parameter twice (or even once really) is best removed entirely if you just always call it using temporaries or const methods only!

The final step is to use the {fmt} library for a better message with a formatter:

#define FMT_HEADER_ONLY
#define FMT_UNICODE 0
#include "fmt/bundled/format.h"
...
constexpr int EXIT_ABORT = 2;
...
// https://godbolt.org/z/3sdx5xPE5
#define ASSERT_EQ(LHS, RHS, MESSAGE ) \
    [lhs=LHS, rhs=RHS, msg=MESSAGE]() { \
        if ( not (lhs==rhs) ) { \
            std::cerr << "assertion " << lhs << "(" #LHS ") == " \
            << rhs << "(" #RHS ") failed: " << msg << '\n'; std::exit(EXIT_ABORT); \
        } \
    }()

I could not get the lambda to capture __VA_ARGS__ , so the call looks like, which is good enough.

ASSERT_EQ(21, life, fmt::format("Expected 21 but life ={}", life));


r/cpp_questions 2d ago

OPEN I'm learning c++ so I have a question, am I supposed to use brace initialization over the = initialization, if so when? I can't decide for myself

45 Upvotes
int x{2};
int x = 2;

r/cpp_questions 2d ago

OPEN The Cengage C++ Textbook is an inefficient way to absorb the material

3 Upvotes

Hello! I currently am taking a C++ class that utilizes Cengage and their textbook environment. My professor stated that the reading is incredibly important for understanding the material. He seemed like a nice guy who cares when I met with him to discuss the class before the semester. Thus-far, I have read two chapters and taken meticulous notes, however I am finding out that it is such an inefficient way to absorb the material.

Although I do understand the concepts for the most part after going through and taking notes, I just wish there was a quicker way to process the textbook, such as an abridged version.

Has anyone experienced this or taken the same class? Please let me know if you have any recommendations or resources that may allow me to progress quickly enough to get ahead of the syllabus.

Thanks in advanced!


r/cpp_questions 2d ago

OPEN Is there any reason to use std::reinterpret_cast?

18 Upvotes

I know that std:: reinterpret_cast is used by people to cast the bits of an object of type T1 to an object of type T2, while retaining the exact bits. However, as far as I know this triggers UB. Before C++20 and std::bit_cast, we could std::memcpy the objects to avoid UB, and after C++20, we can use std::bit_cast. Given this, I don't see any legitimate reason for why one might need std::reinterpret_cast. If so, why hasn't this feature been deprecated yet?


r/cpp_questions 2d ago

META Coroutines

16 Upvotes

Just started reading about coroutines, as the company I recently started working for uses them; this is my first encounter of them. I’m a long time user of threads, bear with me, it’s all new territory.

I know I have only just scratched the surface, but I’m trying to understand the use case. One thing I think I have straight is:

Threads don’t really need to know anything about other threads, except when they have to share resources (mutex and semaphores) or pass data (IPC), but coroutines need to know there are other routines and to only take baby steps, then let other coroutines run.

Is this a start?


r/cpp_questions 2d ago

OPEN How much C++ is enough for getting into Game Development?

23 Upvotes

Hi guys,

I know this question about starting out as a complete noob has been asked and answered numerous times… but my question does have a bit of a twist to it.

How and where do I start learning C++, specifically for rolling into a game engine like Unreal Engine later on? I understand UE C++ is modified a fair bit compared to regular C++, and that I don’t have to become a C++ expert in order to make a game.
As far as I’m aware the hybrid C++ & Blueprint approach is the most efficient way to develop, I don't want to rely solely on Blueprints anyways.

My goal is simply to grasp the core understanding of C++ so I can optimize my game down the road and fix things easier.

I initially wanted to start out with the beloved Bucky’s (thenewboston) C++ tutorial, but as good as it is, people say it’s outdated. Now I’m shifting my focus to learncpp.com.

Until what chapter do you guys think I should learn C++ (the basics and most important parts for game dev) with my end goal in mind?

My end goal is making my own indie horror game, a passion project, not a job I would live off of.
I’ve seen comments saying Chapter 12, and that mostly everything beyond that would have diminishing returns or no real-world use for me.

My Plan:
Learn C++ basics -> Learn the most important parts specifically for Unreal Engine and Game Optimization -> Test games in C++ alone -> Trial and error -> Move over to learning Unreal Engine.

Any help is greatly appreciated!

Important edit: completely forgot to mention, I’m currently on a 1 year internship away from my home setup (10 months left) and all I have currently is a T480 which I used so far mostly for story-writing and planning so I can’t just jump into Unreal Engine.
Just figured might as well learn some C++ basics until I return home and buy a beefy setup and be a little bit more prepared to jump into Unreal Engine.


r/cpp_questions 2d ago

OPEN I'm looking for a AST based cli tool to rename symbols in C++ codebases

2 Upvotes

I want to change existing struct names from snake_case to PascalCase etc. I have a compilation dB and clang tooling. I want to rename one struct from the cli.

How can I do that?


r/cpp_questions 2d ago

OPEN Which one should I start with?

0 Upvotes

I have wanted to learn to program, but I am not sure whether I should learn Python first and then C++. What could you advise me?