void function error

So im trying to create a c++ program that executes the sieve of eratosthenes. I keep getting an error that says I have no matching function to call and it says "candidate function not viable: requires 2 arguments, 1 was provided"

Here's my code:

#include <iostream>
#include <set>

using namespace std;

using std::set;


void sieve(set<int>& s , int n)
{
int prime[n+1];

for(int p=2; p*2<=n; p++)
prime[p] = true;
}

void print_primes(const set<int>& s, int n)
{
int prime[n+1];

for(int p=2; p<=n; p++)
if(prime[p])
cout << p << " ";
}

int main() {

int myInt = 2000;

cout << "Upper limit for the set of primes:" << myInt << endl;

sieve(myInt);

print_primes(myInt);
}
print_primes(myInt);On this line, you are passing in 1 argument, myInt.

void print_primes(const set<int>& s, int n)This is the function that you're attempting to call, but it wants two arguments passed in to it, one that's a set, and one that's an int.

Same issue with sieve.

_________________________

But beyond syntax, think about what your program is currently doing.
You have an array called "prime" (which is illegally allocated as a Variable-Length Array, btw), but you never assign anything to any of prime's elements before checking their values.

If you're going to use an array, then you should have a "bool prime[max_size];" array, initialize all its elements to true, and pass that. And then you need to do the actual sieve logic (see Wikipedia for pseudo-code if you're confused there).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void generate_sieve(bool is_prime[], int n)
{
    is_prime[0] = false;
    is_prime[1] = false;
    // initially set all numbers to true
    for (int i = 2; i < n; i++)
    {
        is_prime[i] = true;
    }

    // ... sieve logic ...
    // ... is_prime[i] = false; ...
}

// ...

const int MaxSize = 2000;
bool is_prime[MaxSize];

generate_sieve(is_prime, MaxSize);


PS: This is a skill that comes with practice, but it is important to give meaningful names to variables. "myInt" is not a meaningful name, because it doesn't describe the purpose of the variable.
Last edited on
Topic archived. No new replies allowed.