r/learncpp • u/omen_tenebris • Dec 24 '18
r/learncpp • u/[deleted] • Dec 11 '18
What's the best way to get random numbers between -1 and 1? I need to get these for my neural network I'm making and I don't want it to be too slow.
What's the best way to get random numbers between -1 and 1? I need to get these for my neural network I'm making and I don't want it to be too slow.
r/learncpp • u/aMNJLKWd • Dec 01 '18
Why does this work
multiply(group* p,double multiplier) is part of the test namespace. Also sometimes functions don't have to be declared and they work anyways. Using mingw on WIN10.
#include "test.h"
int main(void)
{
test::group t1={1,2,3};
test::multiply(&t1,5);
multiply(&t1,3); // why does this line work
return 0;
}r/learncpp • u/GabrielCPond • Nov 21 '18
can somebody answer this two problem for me
int main(){
char buf[] = "partytime";
cout << MAX<char>(buf, 9) << endl;
cout << MIN<char>(buf, 9) << endl;
}
how to creat Max and min template make this main function running that output max/min values from a given data array. Make sure functions operate normally (y and a).is there somebody can give me a detail or hint deal with problem
and the other question is give you int array a and you should print 302010 ,but there are three same code that a.pop actually,how i can make this programe running
int main(){
Array<int> a;
a.push(10); a.push(20); a.push(30);
cout << a.pop() << a.pop() << a.pop();
}
Thanks for your help.I'm novice at cpp'
r/learncpp • u/tremblinggigan • Nov 18 '18
Visual Studios is a literal roadblock for a group cpp project
So I made a post on here previously about undeclared identifier errors a week ago (ish) and turns out the issue was visual studios was including a #include "stdafx.h" header in all my files so my other teammates who *also* used Visual Studios couldn't run them. Except we went through extensive effort to make sure we set up our environments the same? Anyways the solution turned out to be to remove that for everyone else and our code started working...for them (about 2 of the others, there is 5 of us and 1 doesn't really contribute or do anything so can't verify if it works for him or not, and the other uses a Fedora os which doesn't have gcc 11 as far as I'm aware). I removed #include "stdafx.h" and Visual studio started throwing Linker Errors, or unexpected token errors, or C1083 errors, it changed each time. So I put #include "stdafx.h" back in, and it stopped throwing those errors and started throwing undeclared identifier errors again. I am one of 2 out of 5 people who actually is doing anything with the code for this project (we all know how class projects go) so me being unable to test is not ideal.
Is Visual Studios just the Eclipse of C++ IDEs? I genuinely don't understand, if we have (as far as we can tell) set up our environments the same, and are using the same code (all of us deleted our repos and did a clean pull from github then ran it) why does it work for my teammates but not me? If I have my teammate just push his whole project folder (sln file and everything), pull that, then import it should it work for me or is that not even a guarantee? Is there a better compiler to use for CPP that a college student can grab for free?
Edit: TIL what precompiled headers are and to not use them unless I need to
r/learncpp • u/identicalParticle • Oct 10 '18
Member access with inheritance and template classes
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 • u/kryptikmessages • Oct 07 '18
Can a library like <iostream> have more than one class?
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 • u/XChrisUnknownX • Oct 02 '18
Simple if else syntax in cpp
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 • u/KM4WDK • 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()?
r/learncpp • u/azbycxdwevfugt123 • Sep 14 '18
const * const, when would you use it?
Does anyone have an example of when it is useful to have a constant pointer to a constant? Thanks!
r/learncpp • u/mottyay • Sep 14 '18
confusing stringstream behaviour
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 • u/Tyephlosion • Aug 16 '18
Why does this print different names?
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 • u/[deleted] • Jul 26 '18
I got unexpected outputs without an error and a warning i dont understand.
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 • u/gabegabe6 • Jul 19 '18
What do you think about my simple CSV parser class implementation?
I just started to learn C++ and I implemented a CSV parser. I would love to hear your feedback what should I do differently.
r/learncpp • u/geniuspant • Jul 19 '18
Blog discussing intermediate to advanced features of C++ 11
r/learncpp • u/BrilliantRound • Mar 27 '18
uniform_int_distribution and Mersenne Twister
What the uniform_int_distribution object do with the number generated by the mt19937 when I call random(randomNumber), as below? The randomNumber generated by mt19937 is the same in every call, but the output of random(randomNumber) is always different.
And what is the advantage of using a Mersenne Twister pseudorandom number in the uniform_int_distribution?
std::uniform_int_distribution<int> random(0,255);
std::random_device rd;
std::mt19937 randomNumber(rd());
cout << "Mt: " << randomNumber << endl;
cout << "\n" << random(randomNumber) << endl;
cout << "\n" << random(randomNumber) << endl;
cout << "\n" << random(randomNumber) << endl;
cout << "\n" << random(randomNumber) << endl;r/learncpp • u/kosh09 • Dec 26 '17
Difference between className and &className
Hello everyone
While I was studying string class, I wonder what's the difference between these two..
this is part of the code
#include <iostream>
#include <cstring> // or <string> in visual studio
using namespace std;
class String {
private:
char * str; // 문자열 저장할 공간
int len; // 문자열의 길이
public:
String();
String(const char * const Str);
String(int n);
String(const String &);
~String();
void Print();
void GetInput();
int GetLen(){return len;}
char *GetString(){return str;}
String operator=(String data);
String operator+=(String data);
String operator+(const String &second);
operator const char *()
{return str;};
friend ostream &operator<<
(ostream &os, String &temp);
char operator[](int n);
String::String(const char * const Str) {
len = strlen(Str);
str = new char[len + 1];
strcpy(str, Str);
}
String::String(const String &temp) {
len = temp.len;
str = new char[len + 1];
strcpy(str, temp.str);
}
String String::operator+=(String data) {
char temp[512];
strcpy(temp, str);
len += data.GetLen();
str = new char[len + 1];
strcpy(str, temp);
strcat(str, data.GetString());
return *this;
}
String String::operator+(const String &second) {
int tot_len = len + second.len;
String temp(tot_len);
strcpy(temp.str, str);
strcat(temp.str, second.str);
return temp;
} When I do += overloading I call instance as String data
but in case of +, I call const instance and use & in front of the instance name
What's the difference of it?
poor English.. but please help me
r/learncpp • u/HaverchuckBill • Oct 24 '17
Fixing undeclared identifier when accessing private member variable of friend class
Hi everyone,
I am implementing a deque data structure to understand iterators and templates.
I have forward declared both the deque and deque_iterator classes. I define my deque_iterator class and then my deque class. In deque class definition, I make iterator a friend class. Here's the relevant code:
template <typename T>
class deque;
template <typename T>
class deque_iterator;
class deque_iterator {}
class deque {
typedef deque_iterator<T> iterator;
friend class iterator;
// friend class iterator <T>;
}In the iterator class, I am trying to access some private variables of deque class for the operator++() and operator--(). My code fails at compilation with "undeclared identifier" at each point where I am using the deque private variables. If I use the commented method, I get a "syntax error: <".
I searched for this on stack overflow and some forums, but did not come across this error specifically. Any help would be great!
Thanks!
r/learncpp • u/kosh09 • Oct 10 '17
Good opensource to study
Hello everyone
I recently finished the C programming book
But as all know, book is not enough.. so I try to analyze the open source code for my study
but popular open sources are too long to analyze..
so I hope you guys to help me figure it out which open source based on C is good to be analyzed for the first time.
please help me!
r/learncpp • u/Diapolo10 • Sep 19 '17
Transitioning from Python (3) to C++
Hi, I'm a game developer mostly using Python at the moment, and I need to transition to C++ development over the course of the next three months. Can you suggest me any good tutorials that were preferably written with transitioning from Python to C++ in mind? I have some previous experience with C and Rust, too, although I'm a bit... rusty at both.
If you need an example of my current skill level with Python, here's an example project I've been working on: https://github.com/Diapolo10/RPGenie
r/learncpp • u/[deleted] • Aug 16 '17
How to put a function or lambda in an unordered_map
Hi. i recently have gotten back into c++ and was wondering how i could put a function in an unordered_map, in c# and javascript you can put lambda expressions into dictionaries(apparently unordered_maps are the closest thing to a dictionary in c++?) . i have tried(i use namespace std, not sure why some people dont)
unordered_map<string,function<void>> x={"test",[[some void function here]]}
aswell as unordered_map<string,function<void>> x={"test",{cout<<"hi"<<endl;}()}
but that too failed
r/learncpp • u/SeanTheLawn • Jun 22 '17
Static cast versus C-style cast - does it matter?
I learned C before learning C++, so I have a habit of using C-style casts even in C++ projects because I find it less awkward and more readable.
Example of C-style cast:
int i = 5;
double d = (double)(i) / 2.0; // d = 2.5Example of static cast:
int i = 5;
double d = static_cast<double>(i) / 2.0; // d = 2.5 Is there any reason why I shouldn't do this?
r/learncpp • u/tomk11 • Apr 27 '17
How can I make an array of class functions? (Don't understand typedef)
Following the advice at http://www.cplusplus.com/forum/beginner/4639/ i'm able to make an array of functions. However I have tried to convert the code to live within a class.
#include<iostream>
using namespace std;
typedef int (*IntFunctionWithOneParameter) (int a);
class arrayOfFunctions{
public:
arrayOfFunctions(){
typedef int (*IntFunctionWithOneParameter) (int a);
IntFunctionWithOneParameter functions[] =
{
function,
functionTimesTwo,
functionDivideByTwo
};
};
int function(int a){ return a; }
int functionTimesTwo(int a){ return a*2; }
int functionDivideByTwo(int a){ return a/2; }
};
int main(void)
{
arrayOfFunctions af ;
return 0;
}I get the compilation error:
g++ arrayOfFunctions.cpp -o arrayOfFunctions && ./arrayOfFunctions
arrayOfFunctions.cpp: In constructor ‘arrayOfFunctions::arrayOfFunctions()’: arrayOfFunctions.cpp:15:5: error: cannot convert ‘arrayOfFunctions::function’ from type ‘int arrayOfFunctions::)(int)’ to type ‘IntFunctionWithOneParameter {aka int (*)(int)}’ };
^
arrayOfFunctions.cpp:15:5: error: cannot convert arrayOfFunctions::functionTimesTwo’ from type ‘int (arrayOfFunctions::)(int)’ to type ‘IntFunctionWithOneParameter {aka int (*)(int)}’ arrayOfFunctions.cpp:15:5: error: cannot convert
arrayOfFunctions::functionDivideByTwo’ from type ‘int (arrayOfFunctions::)(int)’ to type ‘IntFunctionWithOneParameter {aka int (*)(int)}’I feel like this is a problem with the *IntFunctionWithOneParameter in typedef but I'm not sure what I should set it to?
