What is this error?

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);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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);

}

vector is part of the std library. So just like cout and cin, you're gonna need to use std::

If you've been using using namespace std; I would recommend getting out of that bad habit and start using std::vector<bool>prime_truth;
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).

#include"Test.h" vs #include<vector>
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.
Last edited on
Hi,

I fixed up a couple of things ...

You can go from here :+)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//#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);

}
Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

int main() {

    std::cout << "Find primes till which number? : ";
    std::size_t till;
    std::cin >> till;

    std::vector<bool> prime_truth(2) ; // [ false, false ]
    prime_truth.resize( till, true ) ; // [ false, false, true, true, true, true ... ]

    // ...

}
Topic archived. No new replies allowed.