I am having some trouble! My goal is to check an input number against a list of prime numbers to see if it is prime (in the list) via the find()
function. I haven't gotten that far yet. This is homework so I have to overload the function operator and do it in this dumb (imho) way. Here is what I have thus far:
using namespace std;
class isprime {
public: isprime() { /*nothing...yet?*/
}
bool operator()(int);
list <int> pnums(1, 2);
private: int expandList(int number);
};
bool isprime::operator()(int number) {
if (pnums.back() < number) {}
}
int isprime::expandList(int number) {
for (int j = pnums.back(); j = number; j++) {
for (int i = 2; i < sqrt(j); i++) {
if (j % i != 0) pnums.push_back(j);
}
}
}
int main() {
isprime pcheck;
int number;
while (cin >> number) {
if (pcheck(number)) cout << number << " is prime!\n";
}
}
Here are my errors:
prime2.cpp:12: error: expected identifier before numeric constant
prime2.cpp:12: error: expected ',' or '...' before numeric constant
prime2.cpp: In member function 'bool isprime::operator()(int)':
prime2.cpp:19: error: '((isprime*)this)->isprime::pnums' does not have class type
prime2.cpp: In member function 'int isprime::expandList(int)':
prime2.cpp:23: error: '((isprime*)this)->isprime::pnums' does not have class type
prime2.cpp:25: error: '((isprime*)this)->isprime::pnums' does not have class type
I don't understand what is going wrong. Could anyone help me out?
list.back
is a method, therefore:list.back()
- Rudolf Mühlbauer