1
votes

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?

1
Revise how class definitions work, in particular how you declare class members. - Kerrek SB
1 error at a time, and try figuring it out yourself. It's much more useful than us coming in and providing you with the corrected version. - Luchian Grigore
google hint: constructor initializer list - Rudolf Mühlbauer
one more: list.back is a method, therefore: list.back() - Rudolf Mühlbauer
and one question, why would you consider it 'dumb': i like function objects! clearly, it is a learning exercise, which are often 'strange', but nevertheless! - Rudolf Mühlbauer

1 Answers

0
votes

The biggest problem is how you are trying use the constructor for the list in your class. If you simply remove (1, 2) from the list declaration in your class, it should compile. Second, if you want to call the constructor of an object in your class, I recommend this method

class isprime{
  public: 
   isprime() : pnums(1,2) { /*nothing...yet?*/ }
   ...
   list <int> pnums;
   ...