Trying sieve of eratosthenes but stuck at start.
What is this error?
In file included from alpha.cpp:1:0:
std_lib_facilities.h: In instantiation of ‘T& Vector<T>::operator[](unsigned int) [with T = bool]’:
alpha.cpp:9:16: required from here
std_lib_facilities.h:88:38: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool, std::allocator<bool> >::reference {aka std::_Bit_reference}’
return std::vector<T>::operator[](i);
#include "std_lib_facilities.h"
#include "vector"
vector<bool>prime_truth;
void push_truth(int n){
for (int i = 0; i < n; ++i)
prime_truth.push_back(true);
prime_truth[0] = false;
prime_truth[1] = false; // 0 and one are not prime numbers by definition
}
int main(){
cout << "Find primes till which number? : ";
int till;
cin >> till;
push_truth(till);
}
I'm not exactly privy in on what functions you have and don't have in std_lib_facilities.h, but I would also guess the error stems from including vector wrong.
Quotation marks are for classes you've written yourself. Brackets are for whatever's included in the standard library for C++14 or 11 (not sure).
Tried all the suggestions, doesn't seem to work.
There is no problem in creating vectors,
the problem is with prime_truth[0] = false;
If I comment out that line, no errors.
Oh and I use g++ compiler from terminal, if that helps.
//#include "std_lib_facilities.h"
#include <iostream>
#include <vector>
std::vector<bool> prime_truth;
void push_truth(int n){
for (int i = 0; i < n; ++i)
prime_truth.push_back(true);
prime_truth[0] = false;
prime_truth[1] = false; // 0 and one are not prime numbers by definition
}
int main(){
std::cout << "Find primes till which number? : ";
int till;
std::cin >> till;
push_truth(till);
}