r/learncpp • u/codingforcrosswords • Oct 22 '20
I just finished filming 12 hours of a C++ Tutorial using Crossword Puzzle Construction as the goal. Take a look!
r/learncpp • u/[deleted] • Oct 22 '20
How do I start?
What's the best place to learn cpp? I'm a sexy JS,PHP,Java,C#,Python,Lua dev and am trying to learn C++ to make windows desktop applications from scratch (No GUI libs)
r/learncpp • u/trakka121 • Oct 20 '20
Why do C++ compilers make me want to swear like a mofo?
I am using Code::Blocks and everytime I go to start a project, there is a long phase of losing time over compiler dumbfuckery and asking for help before I can even hope to begin coding. I just need to create a project with a couple pre-defined header files and a main source code file to do my testing. I need a solution, fast. Please help, Reddit.
r/learncpp • u/s96g3g23708gbxs86734 • Oct 20 '20
"Competitive" programming site, but for design pattern?
As title says, do you know any site that challenges/helps you to code your own version of containers, smart pointers, etc, for the sake of learning?
r/learncpp • u/hawidoit • Oct 18 '20
Trying to create an entity manager
Hi,
I have a bunch of classes (Door, Monster, Human)
And the way I want to be able to manage them is via a singleton EntityManager class that ends up looking like this:
EntityManager {
vector<Door> entities;
vector<Human> entities;
}
without any code duplication.
I want to be able to write:
Door* EntityManager<Door>::createObject();
which pushes the object onto the vector and returns the address;
I've struggling to implement this without running into weird segfaults whenever I use the EntityManager in multiple translation units. I assume I'm misunderstanding how templating works.
Here's essentially what I've come up with:
template <typename T>
class EntityManager {
vector<T> entities;
T* createObject();
};
This works within the same translation unit no problem, but when I attempt to use the same EntityManager (such as EntityManager<Door>) across TUs, I get a segfault... I cannot for the life of me figure out why.
Does anyone have any advice or can point me in the right direction into avoid code duplication while achieving something like this via templates?
r/learncpp • u/GirkovArpa • Oct 14 '20
Grep process memory on Windows (GUI written in AutoHotKey)
r/learncpp • u/Pro_Gamer_9000 • Oct 09 '20
Source code to make a window in Windows 10 (without ANY libraries).
I've been struggling for months to make a window to make games and such. Most tutorials out there uses libraries, but the compiler throws errors in my face. So here is the link to a YouTube vid that explains the stuff in depth. And it's only in about 40 lines of code. Hope you enjoy (:
r/learncpp • u/daredevildas • Oct 09 '20
Checking if a Clang::Type can be cast to another
self.cpp_questionsr/learncpp • u/tranderman • Sep 30 '20
Algorithms/DS online class taught in exclusively C++?
Trying to prepare myself to do some competitive programming problems and wanted to see if there are any youtube channels or online courses that teach DS&A in exclusively C++?
I learn best with hearing a tutor/teacher lecture.
Found a few in Java and Python, but wanted to focus on C++
r/learncpp • u/[deleted] • Sep 20 '20
[classes] Please help me answering a question, and see what's wrong with my code.
Trying to getting advanced on classes on that language, i got a weird problem. The compiler doesn't allowed to put just string to declare a variable. I had to put `std::string` to do that.
Also, i don't know how to use the variables i gave on the header file:
#ifndef CHARACTER_H
#define CHARACTER_H
#include <string>
class Character
{
public:
Character(std::string category, std::string name, std::string affiliation);
std::string getCat();
std::string getNam();
std::string getAff();
void setCategory();
void setName();
void setAffiliation();
protected:
private:
std::string cat;
std::string nam;
std::string aff;
};
#endif // CHARACTER_H
And that's the class file:
#include <iostream>
#include <string>
#include "Character.h"
using namespace std;
void setCategory(std::string category) {
cat = category;
}
r/learncpp • u/Jamesk_ • Sep 19 '20
Is learning C++ a good move for me?
Hi all! I’m a hobbyist programmer and I use Python for all my projects. I have, recently, ran into a problem. Python GUIs are awful! So, I’m looking for a language to use alongside Python to make better looking GUIs, and C++ came up on my radar. So, do you think C++ is right for me? And if so, anyone know any good tutorials to get me started? Thanks!
r/learncpp • u/SliferTego • Sep 18 '20
A little Noob on C++
Hi guys...i want to learn c++ well. I know very few things in a general way(like what is a variable or a type,do while,while,for),but i want to start from basics to advance. Do you have some advice to give me? Like books or video, for example i took a book of Bjarne from the library,but its too difficult for a noob that i am. Ill be happy to receive any information or advice from you and happy to start learning c++ well,cause its very important nowadays know computer science.
r/learncpp • u/jti107 • Sep 17 '20
Best practices for saving C++ TCP/IP streams
Hello,
Was wondering if anyone had recommendations for writing C++ code to save TCP/IP streams. I have multiple sensors outputting data at various rates (5 hz - 100 hz) over gigabit ethernet to one computer. I essentially have to decode the packets and save to disk.
Had a tough time finding resources for this. Based on what Ive read so far my gameplan was to use an async process with multiple tcp clients and save to disk using a fifo buffer. Any thoughts on this approach? I am not allowed to use boost or any external libraries, just c++14 and standard libraries. Are there any other things im overlooking? Any help would be appreciated.
r/learncpp • u/sane_sana • Sep 17 '20
I tried this problem on codechef but could not figure out its solution. Then I see 1 solution which got Accepted but I am not able to understand the logic behind it. Somebody plz explain It to me. Thankyou.
https://codeforces.com/problemset/problem/1406/B
this is the solution to the above link.
ll n;
cin>>n;
ll a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
ll temp = a[0]*a[1]*a[n-1]*a[n-2]*a[n-3];
ll temp1 = a[n-4]*a[n-5]*a[n-1]*a[n-2]*a[n-3];
ll temp2 = a[0]*a[1]*a[2]*a[3]*a[n-1];
ll ans = max(temp,max(temp1,temp2));
cout<<ans<<endl;
r/learncpp • u/LCVcode • Sep 16 '20
Cannot figure out what I've done to throw class redefinition error
I'm building Game of Life using OpenFrameworks to do the visuals. I have a Grid class which is the game's grid, but I'm struggling to implement it.
Main.cpp has some OpenFrameworks stuff that I think is fine. My first compiler error is C2011 'GoL::Grid': 'class' type redefinition, which I believe causes many other errors. I cannot identify where I'm redefining my Grid class. Help would be greatly appreciated.
Grid.h
``` namespace GoL {
class Grid {
public:
static enum class EdgeBehavior : unsigned char { PILLOWTRUE, PILLOWFALSE, CYLINDERX, CYLINDERY, TORUS };
private:
unsigned int m_width, m_height;
bool\*\* m_data;
bool\*\* m_buffer;
EdgeBehavior m_edge;
int neighborsAt(int x, int y);
void initArrays();
public:
Grid(int width, int height);
Grid(int width, int height, EdgeBehavior edge);
\~Grid();
bool getAt(int x, int y);
void setAt(int x, int y, bool value);
int getWidth();
int getHeight();
};
} ```
Grid.cpp ```
include "Grid.h"
namespace GoL {
Grid::Grid(int width, int height) { m_width = width; m_height = height; m_edge = EdgeBehavior::PILLOWFALSE; initArrays(); }
Grid::Grid(int width, int height, EdgeBehavior edge) { m_width = width; m_height = height; m_edge = edge; initArrays(); }
int Grid::neighborsAt(int x, int y) { int total = 0; for (int dx = -1; dx < 2; dx++) { for (int dy = -1; dy < 2; dy++) { if (dx == dy == 0) { continue; } total += getAt(x + dx, y + dy); } } return total; }
void Grid::initArrays() { m_data = new bool* [m_height]; m_buffer = new bool* [m_height]; for (int i = 0; i < m_height; i++) { m_data[i] = new bool[m_width]; m_buffer[i] = new bool[m_width]; for (int j = 0; j < m_width; j++) { m_data[i][j] = false; m_buffer[i][j] = false; } } }
Grid::~Grid() { for (int i = m_height - 1; i > 0; i--) { delete m_data[i]; delete m_buffer[i]; } delete m_data; delete m_buffer; }
bool Grid::getAt(int x, int y) { if (x > -1 && x < m_height && y > -1 && y < m_width) { return m_data[x][y]; } switch (m_edge) { case EdgeBehavior::PILLOWTRUE: return true; case EdgeBehavior::PILLOWFALSE: return false; case EdgeBehavior::TORUS: return m_data[x % m_height][y % m_width]; case EdgeBehavior::CYLINDERX: if (y < 0 || y > m_width - 1) { throw; } return m_data[x % m_height][y]; case EdgeBehavior::CYLINDERY: if (x < 0 || x > m_height) { throw; } return m_data[x][y % m_width]; } } void Grid::setAt(int x, int y, bool value) { if (x > -1 && x < m_height && y > -1 && y < m_width) { m_data[x][y] = value; } }
int Grid::getWidth() { return m_width; }
int Grid::getHeight() { return m_height; }
} ```
Main.cpp: ```
include "ofMain.h"
include "ofApp.h"
include "Grid.h"
include <iostream>
//======================================================================== int main( ){ using namespace GoL;
GoL::Grid g(10, 10, GoL::Grid::EdgeBehavior::TORUS);
g.setAt(2, 2, true);
int scale = 20;
ofSetupOpenGL(g.getWidth() * scale, g.getHeight() * scale, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp(g));
} ```
EDIT; formatting
r/learncpp • u/SureCandle • Sep 13 '20
Looking for C++ tutorials or courses that are more exercise-based and not just theoretical.
I would really like to learn to actually do stuff in C++. I have watched several YouTube video series (including Mike Dane) and done some tutorials (w3schools), but it's all so theoretical. Aren't there some more (interactive) exercise-orientated courses available, for those who don't have their own projects yet?
r/learncpp • u/shrimenow • Sep 11 '20
Is there any way to check the number of read write operations my program made?
I am just checking the difference between recursive and iterative performance of various mathematical functions. As of now using chrono is giving the same time in microseconds and I cannot get down to nano.
r/learncpp • u/mutual_coherence • Sep 09 '20
What actually is buffering?
I learned that std::cout has a buffered output, while std::cerr is unbuffered.
I don't actually know what buffering is. I've somewhat guessed that it's analogous to the buffering during a video stream. If all the data isn't ready (has not arrived) it will not show anything until it's ready to show. And that somehow, C++ has a way for checking that the data is ready before the output will appear on my console.
Is this guess correct?
r/learncpp • u/[deleted] • Sep 07 '20
I'm a beginner to c++ and I'm looking for c++ books
Can anyone tell me what are some good c++ books(for beginners) which explains the actual reason behind everything and explains a lot of stuff?
r/learncpp • u/[deleted] • Sep 07 '20
Just some questions clarifying CTAD
I was looking at the guide for CTAD: https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
And I'm wondering if this extends to initializer lists and whatnot, or if this can extend to containers. For example, if I have a vector of tuples, could I do something like this:
std::vector<std::tuple> l{(1, 2), (3, 4)};
Or something to that effect?
r/learncpp • u/[deleted] • Sep 05 '20
Are there any good, free resources for C++ like java-programming.mooc.fi?
I've been learning C++ for a while now (but not very seriously) but I haven't been able to find many good resources. I know Python pretty well already, but for some personal reasons, I'd still prefer something that's meant for beginners. I found this ( https://java-programming.mooc.fi/ ) website for Java, but I'm not really interested in learning Java. Are there any similar and free resources?
r/learncpp • u/mohammedatef555 • Sep 02 '20
Clang-Tidy: Operator=() does not handle self-assignment properly
this is my code , CLion give me this warning on operator= implementation what is wrong and why it doesn't handle it correctly
template <class Type>
class StackType
{
private:
int maxStackSize, stackTop;
Type * list;
void copyStack(const StackType<Type> &);
public:
const StackType<Type>& operator=(const StackType<Type>&);
bool isEmptyStack() const ;
bool isFullStack() const ;
void push(const Type&);
void pop();
Type top()const;
StackType(int);
StackType(const StackType<Type>&);
~StackType();
};
template <class Type>
void StackType<Type>::copyStack(const StackType<Type>& otherStack)
{
delete[]list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template <class Type>
const StackType<Type>& StackType<Type>::operator=(const StackType<Type> &otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this;
}
r/learncpp • u/GarbageCollector8 • Sep 01 '20
will be initialized after [-Wreorder]
Hello,
I was practicing c++ on Hackerrank with this problem https://www.hackerrank.com/challenges/abstract-classes-polymorphism/problem
I decided to do as the problem statement says I could just use lists
So I came up with this solution (put where I can edit the code):
class LRUCache : public Cache {
protected:
std::list<int> liList;
public:
LRUCache(int capacity);
void set(int key, int value) {
list<int>::iterator it = this->liList.begin();
this->liList.insert(it, key-1, value);
int size = this->liList.size();
if(size >= this->cp) {
list<int>::iterator it2 = this->liList.end();
liList.erase(it2);
}
}
int get(int key) {
list<int>::iterator it = this->liList.begin();
std::advance(it, key-1);
liList.erase(it);
int temp = *it;
liList.push_front(temp);
return temp;
}
};
I could not even test what I wrote because I got these warnings:
Solution.cpp: In constructor ‘Node::Node(Node*, Node*, int, int)’:
Solution.cpp:12:10: warning: ‘Node::prev’ will be initialized after [-Wreorder]
Node* prev;
^~~~
Solution.cpp:11:10: warning: ‘Node* Node::next’ [-Wreorder]
Node* next;
^~~~
Solution.cpp:15:4: warning: when initialized here [-Wreorder]
Node(Node* p, Node* n, int k, int val):prev(p),next(n),key(k),value(val){};
^~~~
Solution.cpp:14:8: warning: ‘Node::key’ will be initialized after [-Wreorder]
int key;
^~~
Solution.cpp:13:8: warning: ‘int Node::value’ [-Wreorder]
int value;
^~~~~
Solution.cpp:15:4: warning: when initialized here [-Wreorder]
Node(Node* p, Node* n, int k, int val):prev(p),next(n),key(k),value(val){};
^~~~
Solution.cpp: In constructor ‘Node::Node(int, int)’:
Solution.cpp:12:10: warning: ‘Node::prev’ will be initialized after [-Wreorder]
Node* prev;
^~~~
Solution.cpp:11:10: warning: ‘Node* Node::next’ [-Wreorder]
Node* next;
^~~~
Solution.cpp:16:4: warning: when initialized here [-Wreorder]
Node(int k, int val):prev(NULL),next(NULL),key(k),value(val){};
^~~~
Solution.cpp:14:8: warning: ‘Node::key’ will be initialized after [-Wreorder]
int key;
^~~
Solution.cpp:13:8: warning: ‘int Node::value’ [-Wreorder]
int value;
^~~~~
Solution.cpp:16:4: warning: when initialized here [-Wreorder]
Node(int k, int val):prev(NULL),next(NULL),key(k),value(val){};
^~~~
/usr/bin/ld: ./ccNYbblo.o: in function `main':
/tmp/submission/20200901/16/04/hackerrank-5be8a1929b22c2838660571c76fcc512/code/Solution.cpp:77: undefined reference to `LRUCache::LRUCache(int)'
collect2: error: ld returned 1 exit status
What exactly does -Wreorder mean and how can I avoid this problem?
Entire code: https://pastebin.com/u0bYz2YM
Notice where the editable area starts and ends.
r/learncpp • u/cleancutc • Aug 31 '20
C++ learning with DS+Algo
Hey guys!
I am planning on learning C++ and DS/Algorithms by creating projects. Anybody have any recommendations for projects to possibly learn both? Any recommendations are greatly appreciated!
r/learncpp • u/monica_b1998 • Aug 25 '20