I'm using a class to generate a list of primes.
Im checking a series of numbers being incremented by two
against the current vector of primes to see if any primes divide
into the number evenly.

If no primes divide into it I add the number onto the end of the list of primes
and continue until I have reached an inputted value.

IT is suposed to output every prime up to the inputted value

``````#include<iostream>
#include<iomanip>
#include<vector>
#include<stdexcept>
using namespace std;

class Primelist
{
private:
void Populate(float limit);
public:
long val;
bool flag;
int intval;
float longval;
float convert;
vector<float> primelist;
vector<float>::iterator prime_Iter;
bool Primechecker(int);
bool Primelistcheck(float subject);
void Print();
Primelist(float limitconstruct);
};
//------------------------------------------------------------------------------
int main()
{
float input;
cout << "Enter the limit of your prime search.\n";
cin >> input;
Primelist primes(input);
primes.Print();
cin.get();
cin.get();
return 0;
}

//------------------------------------------------------------------------------
Primelist::Populate(float limit)
{
for (float p=2; p<20; p++ )
{
if ( Primechecker(p) == true )
primelist.push_back(p);
}

for ( float i=21; i<val ;i+=2 )

{
flag = true;
flag = Primelistcheck(i);
if (flag = true)
primelist.push_back(i);
}

}

Primelist::Primelistcheck(float subject)
{
for ( prime_Iter = primelist.begin(); prime_Iter != primelist.end(); prime_Iter++ )
{
longval = i/ *prime_Iter;
intval = longval;
convert = intval;
if (longval == convert)
{
return false;
}
return true;
}

Primelist::Print()
{
for (int c=0; c<primelist.size(); c++)
{
cout << primelist[c] << endl;
}
}

Primelist::Primechecker(int num)
{
for (int i=2; i< num/2+1; i++)
{
if (num % i == 0)
return false;
}
return true;
}
Primelist::Primelist(float limitconstruct)
{
val = limitconstruct
flag = true;
intval = 0;
longval = 0;
convert = 0;
vector<float> primelist;
vector<float>::interator prime_Iter;
Populate(val);
}``````

My compiler tells me that I cannot declare the function Primelist(float) without a type
and that the prototype for it does not match any in class 'Primelist'

Any help would be appreciated

## All 6 Replies

I Wrote that Program To!!!

i really hated it though :(

My compiler tells me that I cannot declare the function Primelist(float) without a type...

`main()` is an int type. What type did you define `Primelist(float)` ?

... and that the prototype for it does not match any in class 'Primelist'

Well, how is it defined and how is it used? Are all the parameters of the same type?

you are making this way too complicated. to check if an integer is divisible by another, use the % operator instead of floating points.

> ..against the current vector of primes to see if any primes divide into the number evenly.
you need to check only up to the square root of the number. ie to check if 179 is a prime number, you only need to try dividing by 3, 5, 7, 11, 13

``````#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <cassert>
using namespace std;

struct prime_list
{
explicit prime_list( int limit ) ;
std::vector<int> primes ;
private: bool is_prime( int number ) const ;
};

bool prime_list::is_prime( int number ) const
{
int root = int( sqrt( double(number) ) ) + 1 ;
assert( primes.back() >= root ) ;
for( size_t i=1 ; i<primes.size() && primes[i]<=root ; ++i )
if( number%primes[i] == 0 ) return false ;
return true ;
}

prime_list::prime_list( int limit )
{
if( limit > 2 ) primes.push_back(2) ;
for( int number=3 ; number<limit ; number+=2 )
if( is_prime(number) ) primes.push_back(number) ;
}

int main()
{
prime_list list(500) ;
copy( list.primes.begin(), list.primes.end(),
ostream_iterator<int>(cout,"\n") ) ;
}``````

I was trying to define Primelist(float) as an alternate constructor so I could define the limit of the primelist, and as far as I know you don't declare a return type for constructors in the class definition. I used floating point numbers because the limits I will need to use will cause overflow using int type, and I thought it would be simple to determine wether a given float variable is a whole number or not.
THanks for the tip on searching only to the square root, didn't know that.

Simplified and rewrote whole thing it was way to complicated.
Assignment is to do it without using anything but vectors and std
I think this should work alot better but i get an assertion error when I run the exe
that says the vector subscript is out of range.
Any ideas?

``````#include<iostream>
#include<iomanip>
#include<vector>
using namespace std;

class Primelist
{
public:
vector<long> primes;
void Start();
void Print();
void Populate(long limit);
};
//------------------------------------------------------------------------------
int main()
{
long limit;
cout << "Enter the limit of your search: ";
cin >> limit;
cout << endl << endl;
Primelist primes;
primes.Start();
primes.Populate(limit);
primes.Print();
cin.get();
cin.get();
return 0;
}

//------------------------------------------------------------------------------
void Primelist::Populate(long limit)
{
for (int i=7; i< limit; i++)
{
vector<long>::iterator prime_Iter;
bool flag = true;
for (prime_Iter=primes.begin(); *prime_Iter * *prime_Iter < i; prime_Iter++)
{
if ( i % *prime_Iter == 0 )
flag = false;
}
if (flag = true)
{
primes.push_back(i);
}
flag = true;
}
}
void Primelist::Start()
{
for (int i=0; i<3; i++)
{
if (i<2)
{
primes[i]=i+2;
}
else
primes[i]=5;
}
}
void Primelist::Print()
{
for (int c=0; c<primes.size(); c++)
{
cout << setw(5) << "1" << setw(8) << "1" << endl;
cout << setw(5) << c+2 << setw(8) << primes[c] << endl;
}
}``````

> as far as I know you don't declare a return type for constructors
explicit is a c++ keyword used to specify that a constructor cannot be used for implicit conversions. it is a declaration specifier (not a type). see http://cprogrammers.blogspot.com/2005/11/c-programming-explicit-constructors.html

> I thought it would be simple to determine wether a given float variable is a whole number or not.
it is (perhaps surprisingly) complicated. try out this program:

``````#include <iostream>

int main()
{
std::cout << std::fixed << std::showpoint << std::boolalpha;
float a = 1234567890.0 ; std::cout << a << '\n';
float b = a+40 ; std::cout << b << '\n';
float c = a-40 ; std::cout << c << '\n';
std::cout << "a==b? " << (a==b) << '\n' ;
std::cout << "(a+40)==a? " << ( (a+40)==a ) << '\n' ;
std::cout << "a==c? " << (a==c) << '\n' ;
std::cout << "b==c? " << (b==c) << '\n' ;
}``````

> i get an assertion error when I run the exe that says the vector subscript is out of range.
line 37: you are going out of range for small values of i. also, the number you are looking at may be a square of a prime number; so the < should be changed to a <=

``````for (prime_Iter=primes.begin();
(prime_iter != primes.end() ) && (*prime_Iter * *prime_Iter <= i );
prime_Iter++)``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.