'expected primary-expression before ‘)’ token' for default_random_engine - c++

I try my local compiler and online http://www.compileonline.com/compile_cpp11_online.php. Both generates the same errors.
#include <iostream>
#include <random>
using namespace std;
int main()
{
default_random_engine gen((random_device())());
cout << "Hello World" << endl;
return 0;
}
I have used g++ to compile the above code and the error is as follows:
error: expected primary-expression before ‘)’ token
g++ $1.cpp -o $1 -g -Wall -std=c++0x // error
However, I don't have such an issue with clang.
clang++ -o $1 -Werror $1.cpp -std=c++11 -O3 // fine
Question> can someone help me figure it out why? and how to correct it under g++ since I have to run g++ to debug my code.
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
clang version 3.4 (trunk 185180)
// Updated //
//http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
#include <random>
#include <iostream>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 6);
for (int n=0; n<10; ++n)
std::cout << dis(gen) << ' ';
std::cout << '\n';
}

Related

this_thread / chrono has not been declared

I have checked many StackOverflow posts, but no answers solve my problem.
I get 2 errors:
g++ .\main.cpp -fopenmp -o test
.\main.cpp:12:14: error: 'std::this_thread' has not been declared
12 | std::this_thread::sleep_for(chrono::seconds(20000) );
.\main.cpp:12:37: error: 'chrono' has not been declared
12 | std::this_thread::sleep_for(chrono::seconds(20000) );
My current G++ version is:
g++.exe (MinGW.org GCC Build-2) 9.2.0
The code is:
#include <iostream>
#include <chrono>
#include <thread>
#include <omp.h>
int main()
{
omp_set_num_threads(4);
#pragma omp parallel
{
std::this_thread::sleep_for(chrono::seconds(20000) );
std::cout << "Number of available threads: " << omp_get_num_threads() << std::endl;
std::cout << "Current thread number: " << omp_get_thread_num() << std::endl;
std::cout << "Hello, World!" << std::endl;
}
return 0;
}
I have already tried -std=c++11 from 11 & 14 & 17.
I'm not sure it's right but about your second error can you replace it with
std::this_thread::sleep_for(chrono::seconds(20000) );
to
std::this_thread::sleep_for(std::chrono::seconds(20000));
I think the first error depends from second, but I'm not sure.

C++ stoi not resolving using eclipse to compile

This code fails to compile with an error that it can't resolve stio. Have I made some newbie mistake here?
Eclipse Version: 3.8.1 Mint KDE should all be up to date.
GCC Version: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
string numberGuessed;
int intNumberGuessed = 0;
int answer = 0;
answer = (rand() % 100) + 1;
do {
cout << "Guess a number "; // prints !!!Hello World!!!
getline(cin, numberGuessed);
intNumberGuessed = stoi(numberGuessed);
cout << "You guessed "<< numberGuessed << endl;
cout << "You are not correct. Try again" << endl;
} while (answer != intNumberGuessed);
cout << "you got it";
return 0;
}
The error message.
16:39:14 **** Incremental Build of configuration Debug for project Hello2 ****
make all
Building file: ../src/Hello2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Hello2.d" -
MT"src/Hello2.d" -o "src/Hello2.o" "../src/Hello2.cpp"
../src/Hello2.cpp: In function ‘int main()’:
../src/Hello2.cpp:27:40: error: ‘stoi’ was not declared in this scope
intNumberGuessed = stoi(numberGuessed);
^
make: *** [src/Hello2.o] Error 1
src/subdir.mk:18: recipe for target 'src/Hello2.o' failed
16:39:14 Build Finished (took 613ms)
The std::stoi function is available since the c++11 standard.
Apparently your compiler version of GCC is too old, to take c++11 as the current default standard.
You may try to specify the -std=c++11 or -std=c++0x compiler flags, or update your gcc compiler to one of the most recent versions.
Here's a link explaining in detail how to set the compiler flags.
This might help you with updating your compiler version to the latest.

C++ Thread using Eclipse

So I'm working on a project for a class and I cannot seem to get things to work. 1) Did I do this right? 2) How do I get rid of the errors?
#include <iostream>
#include <thread>
using namespace std;
void countdown(){
int count;
count = 21;
while (count<=0)
{
count--;
cout << "Count is " << count << '.' << endl;
}
}
int main() {
std::thread t1(countdown);
t1.join();
int count1;
count1 = 0;
while (count1<20)
{
count1++;
cout << "Count is " << count1 << '.' << endl;
}
return 0;
}
Error messages:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -Wl,--no-as-needed -o "src\\Critical7.o" "..\\src\\Critical7.cpp"
..\src\Critical7.cpp: In function 'int main()':
..\src\Critical7.cpp:27:2: error: 'thread' is not a member of 'std'
std::thread t1(countdown);
^
..\src\Critical7.cpp:28:2: error: 't1' was not declared in this scope
t1.join();
I've tried setting things the way other posts have said but I can't seem to get it to work.
Two modifications are necessary to run the code correctly:
replace while (count<=0) with while(count>=0), else the loop in countdown() exits immediately.
Use the -pthread linker option in order to compile the code.

Parsing LLVM IR from bitcode file

I am trying to parse LLVM IR from a bit code file. I went through the following steps.
hello.cpp
#include <iostream>
int main() {
std::cout << "Hello world!" << "\n";
return 0;
}
dump.cpp
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
using namespace llvm;
int main()
{
LLVMContext context;
SMDiagnostic error;
Module *m = parseIRFile("hello.bc", error, context).get();
if(m)
{
m->dump();
}
return 0;
}
$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc
$ clang++ -g -O3 dump.cpp llvm-config --cxxflags --ldflags --system-libs --libs all -o dump
$ ./dump
Assertion failed: (Val && "isa<> used on a null pointer"), function doit, file /Users/chamibuddhika/Builds/llvm/include/llvm/Support/Casting.h, line 106.
Abort trap: 6
So I get the above error at the end. What may be causing this? I am on llvm-6.0 rc2.

openssl error "EVP_DigestInit_ex:initialization error" using EVP-library

I have a problem with openssl and the EVP-functions:
When I execute the following code
#include <openssl/evp.h>
#include <openssl/engine.h>
#include <array>
#include <iostream>
void hash(ENGINE* eng) {
EVP_MD_CTX *_mdctx(EVP_MD_CTX_create());
int ret = EVP_DigestInit_ex(_mdctx, EVP_sha512(), eng);
EVP_MD_CTX_destroy(_mdctx);
if(1 == ret) {
std::cout << "Finished successfully (with eng=" << eng << ")" << std::endl;
return;
} else {
std::array<char, 256> err_str;
ERR_error_string_n(ERR_get_error(), err_str.data(), err_str.size());
std::cout << "Error at Digest (engine: " << ENGINE_get_id(eng) << "): " << err_str.data() << std::endl;
}
}
int main(void) {
ENGINE_load_builtin_engines();
hash(nullptr);
for(ENGINE *eng = ENGINE_get_first(); eng != nullptr; eng = ENGINE_get_next(eng)) {
hash(eng);
}
}
I get the following output:
Finished successfully (with eng=0)
Error at Digest (engine: rdrand): error:260BA093:engine routines:ENGINE_get_digest:unimplemented digest
Error at Digest (engine: dynamic): error:06080086:digital envelope routines:EVP_DigestInit_ex:initialization error
I understand, that rdrand doesn't support digest, but why do I get an initialization error, when I use the dynamic engine? In particular why does it work, when I call EVP_DigestInit_ex with eng=nullptr?
The code can be compiled with g++ example.cpp -std=c++17 -Wall -Wextra -Werror -pedantic -O2 -lssl -lcrypto. I am using g++, version 6.3.0 and openssl 1.1.0f.