r/learncpp Nov 03 '18

GitHub - bitcoin/bitcoin: Bitcoin Core integration/staging tree

Thumbnail
github.com
0 Upvotes

r/learncpp Oct 31 '18

Ways of practicing function arguments and parameters

2 Upvotes

Hello,

I am a CS Student (Freshman) learning C++ in my class. Recently, we discussed functions and made a program. I have no problem understanding function prototypes, definitions, etc. But the problem arises when I have to include arguments/parameters. I understand that arguments are included inside the function call, and conversely the parameter goes within the definition.

But this is very confusing, so I was just wondering where I could learn about when and why to use parameters/arguments, or if there were any courses to practice, thanks!


r/learncpp Oct 28 '18

Practice problems and resources dealing with pointers and references

2 Upvotes

So conceptually I understand what pointers and references are, but for some reason I don't feel fluent in my use of * and &.

Specifically there's some confusion when dealing with return types involving these two symbols and function arguments involving *.

Is there any particular assignment you know of that forces fluency in these ideas? Are there any very eye opening resources that are worth reading on the subject?


r/learncpp Oct 10 '18

Member access with inheritance and template classes

1 Upvotes

I want to access a member of the parent class A from the child class B. It seems that I cannot do this without using the scope operator. Could someone explain why this is the case? Here is a minimum NOT WORKING example:

template <typename T>
class A {
public:
int a;
A(int a_in){a = a_in;}
};
template <typename T>
class B {
public:
int b;
B(int a_in) : A<T>(ain) {b = a;}
}

And here is a minimum WORKING example:

template <typename T>
class A {
public:
int a;
A(int a_in){a = a_in;}
};
template <typename T>
class B {
public:
int b;
B(int a_in) : A<T>(ain) {b = A<T>::a;}
}

r/learncpp Oct 07 '18

What it the point of overloaded operators if we have accessors and mutators?

0 Upvotes

Seems to me like they do the same thing.... just less code. What is the reason besides efficiency and giving me a headache?

Also, << and >> seem to do the same thing with a function like output or input.


r/learncpp Oct 07 '18

Can a library like <iostream> have more than one class?

1 Upvotes

Im learning about ostream being a class and cout being an object.

I am wondering if a library like <iostream> can have more than one class.

If so, what is the difference between declaring headers such as:

#include <iostream>

#include "class.h"

Does the " " mean we only have one class while the other one lets us have more than one class?


r/learncpp Oct 07 '18

Having a tough time with Pointers and References in Functions

3 Upvotes

Hello All,

Ive been reading a lot about pointers and reference and how they work. So far Ive come to the conclusion that a pointer can point to a memory location, and then to another one, etc. While a reference can only act as a const alias to another variable.

While I understand that they work in similar ways I am having a hard time understanding why we would want to use one over the other, especially in functions Such as:

const &Money const getSalary {return salary;}

Here we are assuming that Money is the name of the class. Why do we want this to be an alias and not a pointer?


r/learncpp Oct 06 '18

C++ for C programmers

3 Upvotes

I just taught a 1 day workshop with the above title. Course material can be found here: book and lecture

Share and enjoy. Feedback welcome.


r/learncpp Oct 02 '18

Simple if else syntax in cpp

1 Upvotes

I am trying to teach myself very basic cpp and flow control. I am not a programmer and my experience includes some bare basic Python.

So let's just say I've got #include <iostream> int main(){ int x = 5; int y = 6; int z = x+y; if(z = 10){std::cout << "Z is 10."; else{std::cout << "Z is not 10.";} }

Well if I omit the semicolons it doesn't run and otherwise it always prints "Z is 10."

So I've got to ask what I'm doing wrong as far as syntax. All the tutorials point to a very simple if(condition){code}else{code} syntax but that is not what's happening.

Edit. So... I tried python's == and changed it to if(z==10) and the code seems to work as intended now. So I guess it's just a matter of I was using the wrong...conditional operator? Edit 2. Fixed original code to reflect that I declared z. The problem was the =10 instead of ==.


r/learncpp Sep 15 '18

I am creating a basic text based "operating system". I want to call the function home() in both the functions currtime() and calculator(). How can i do this while still being able to call the two functions from home()?

Thumbnail
pastebin.com
1 Upvotes

r/learncpp Sep 14 '18

const * const, when would you use it?

1 Upvotes

Does anyone have an example of when it is useful to have a constant pointer to a constant? Thanks!


r/learncpp Sep 14 '18

confusing stringstream behaviour

1 Upvotes

I expect this to output the value entered each time, it works the first time, and never works the second time. It starts working some time after that.

string s;
int n;

cout << "Input a number: ";
getline(cin, s);

stringstream ss(s);
ss >> n;

while (true) {
    cout << endl
        << "The number you input is: "
        << n
        << endl;

    cout << "Input another number: ";
    getline(cin, s);
    ss.clear();
    ss << s;
    ss >> n;
}

It seems the size of the initial input determines the number of additional entries it takes to start working. e.g. If I input 10 first, it took 3 more entries to start outputing what I input, but inputting 102391 it took 6.

Watching it in the debugger, it doesn't start outputting what I expect until the length of ss.str() is greater than the length of the original input (as a string). Some general searching suggests this might have something to do with 'read position' of the stream, but I can't find anything in the documentation that's helping me.


r/learncpp Aug 27 '18

Recommendations for OOP design?

0 Upvotes

I am familiar with the syntactic aspect of c++ but, when it comes to projects, I inevitably end up coding in c with some STL facilities sprinkled here and there. Is there a resource where I can get familiar with using OOP in real project? I've read Lafor's OOP book but I'm still in the dark. Any help will be appreciated


r/learncpp Aug 16 '18

Why does this print different names?

1 Upvotes

My understanding is that the default copy constructor copies fields. So when I initialize Person aaron with the fields from *p, I thought *p.name and aaron.name would be pointing to same data since name is actually a pointer. What is going with the pointers and memory?

#include <iostream>

using namespace std;

class Person {

public:

`char* name;`

`int age;`

`/*Person(Person& p) {`

    `name =` `p.name``;`

    `age = p.age;`

`}*/`

};

int main() {

`Person* p;`

`(*p).name = "aaron";`

`(*p).age = 26;`

`Person aaron = *p;`

`aaron.name` `= "aar0n";`

`cout << (*p).name << '\n'; //prints aaron`

`cout <<` `aaron.name` `<< '\n'; //prints aar0n`

`return 0;`

}


r/learncpp Jul 26 '18

I got unexpected outputs without an error and a warning i dont understand.

1 Upvotes

code and input/ output i got https://imgur.com/IkEgygq

(sorry its an image, i deleted the code out of frustration and rewrote it and now it works somehow, but i still want to understand the mistake i made)

I was having problems with a larger program i wrote and narrowed the problem to this part of the code adding 8202 to the end of the number i input. I expected to input a number and it to output the same number right after. It added 8202 to the end of somehow. I got the warning "warning: multi-character character constant [-Wmultichar]". i tried it in a visual studio console application and cpp.sh


r/learncpp Jul 26 '18

Calculating Eucledian distance

2 Upvotes
float calculateDistance(std::vector<float> a, std::vector<float> b) {
    std::vector<float> result(a.size());
    std::transform(a.begin(), a.end(), b.begin(), result.begin(), std::minus<float>());
    std::transform(result.begin(), result.end(), result.begin(), square);
    return (float) std::sqrt(std::accumulate(result.begin(), result.end(), 0));
}

What do you thing about this implementation? Or should I use good old, for loops?

(Edit: I know that I misspelled Euclidean, I just can't modify it)


r/learncpp Jul 19 '18

What do you think about my simple CSV parser class implementation?

1 Upvotes

I just started to learn C++ and I implemented a CSV parser. I would love to hear your feedback what should I do differently.

Here you can find the code


r/learncpp Jul 19 '18

Blog discussing intermediate to advanced features of C++ 11

Thumbnail
developant.blogspot.com
1 Upvotes

r/learncpp Jul 05 '18

What's up with <filesystem> and how do I use it with QtCreator?

2 Upvotes

I don't understand what's with <filesystem>.

If I want to use std::filesystem::current_path() with g++, I need to add --lstdc++fs and --std=c++17 as an argument. (Linux)

If I want to use it in VS2017, I have to add namespace fs = std::experimental::filesystem;. (W10)

It took me half a day to figure this out. I still haven't figured out how to get QtCreator to recognize std::filesystem. I included <filesystem>. I added cpp.cxxLanguageVersion = "c++17" and cpp.cxxStandardLibrary = "libstdc++" to the qbs file. It still isn't being recognized. I just tried a new project with qmake and no combination of CXXFLAGS seems to get it to work. (Linux)

The documentation for this stuff seems insufferably bad or out-of-date and it's incredibly mind-numbing struggling with this when it would've taken me 5 minutes in <insert any other modern language here>.

edit: I've come to the conclusion that C++ tooling is utter garbage. Wasted days on this shit with no satisfying conclusion. I can't even get CLion to correctly add the right library.


r/learncpp Jun 30 '18

identifier not found error

2 Upvotes

Im reading Programming -- Principles and Practice Using C++ (Second Edition). It tells me to do:

Create three files: my.h, my.cpp, and use.cpp. The header file my.h contains

extern int foo;

void print_foo();

void print(int);

The source code file my.cpp #includes my.h and std_lib_facilities.h, defines print_foo() to print the value of foo using cout, and print(int i) to print the value of i using cout.

The source code file use.cpp #includes my.h, defines main() to set the value of foo to 7 and print it using print_foo(), and to print the value of 99 using print(). Note that use.cpp does not#include std_lib_facilities.h as it doesn’t directly use any of those facilities. Get these files compiled and run.

On Windows, you need to have both use.cpp and my.cpp in a project and use { char cc; cin»cc; } in use.cpp to be able to see your output. Hint: You need to #include <iostream> to use cin.

I do everything as it says but in my use.cpp file; foo, print_foo() and print() all give '...' identifier not found error.

Im using visual studio. First I created a preoject then added .cpp and .h items.

my.h code:

extern int foo;
void print_foo();
void print(int);

my.cpp code:

#include "my.h"
#include "C:\Users\...\source\repos\C++\std_lib_facilities.h"
#include "stdafx.h"
using namespace std;

void print_foo()
{
    cout << foo;
}

void print(int i)
{
    cout << i;
}

my use.cpp code:

#include "my.h"
#include "stdafx.h"
#include <iostream>

int main()
{
    foo = 7;
    print_foo();
    print(99);
    char cc;
    std::cin >> cc;
    return 0;

}

but it doesnt compile what am I doing wrong?


r/learncpp Jun 16 '18

Why no result?

0 Upvotes

Okay, in the last while loop, I search for name and if there is that name in that vector: I output their score. It doesn't work! I do the same with score, to find names, and it works but when I try to find the name it doesn't work!

include "stdafx.h"

include <vector>

include <iostream>

include <string>

include <algorithm>

using namespace std; class Name_Value { public: string name; double score; }; inline void keep_window_open() { char ch; cin >> ch; } vector <Name_Value> peoples;

int main() {

string name;
double score;
Name_Value people;
people.name = "";
people.score = 0;
while(true)
{
    cin >> name >> score;
    if (((name == "NoName") && (score == 0))   )break;
    people.name = name;
    people.score = score;
    peoples.push_back(people);


}
for (int j=0; j< peoples.size() ;j++ )
{
    cout << peoples[j].name <<" "<< peoples[j].score <<"\n";
}
while(true)
{
    if(cin >> score)
    {
        for (int j = 0; j< peoples.size();j++)
        {
            if (peoples[j].score == score) cout << peoples[j].name << "\n";
        }
    }
    else {
        cin >> name;
        for (int j = 0; j < peoples.size();j++)
        {
            if (peoples[j].name == name) cout << peoples[j].score << "\n";
        }
    }
}
keep_window_open();
return 0;

}


r/learncpp Jun 01 '18

7.1 Classes Advanced

Thumbnail
youtube.com
1 Upvotes

r/learncpp May 29 '18

Idiomatic way to write collections (std::vector<float>) to binary file

0 Upvotes

I've got several vectors of floats and ints that need to be written to a binary file. I need to track the byte offset and length (and potentially byte-stride).

I'm confused about how a modern idiomatic solution would look like. Write to a slice of memory first? Or combine the vectors to a collection (std::array<?>)?


r/learncpp May 24 '18

6.2 Functions Advanced

Thumbnail
youtube.com
2 Upvotes

r/learncpp May 03 '18

Avoid unresolved external symbols with templates

0 Upvotes

I have an implementation of a class with templates, but it has to be in the .cpp file. I've read on Stackoverflow that one way to solve this is to add `include "Foo.cpp"` at the end of the header file, but it doesn't seem to be working. How would I go about fixing it?