0

Hi. I'm a Java programmer making the switch to C++. Could someone enlighten me as to what's going wrong here:

g++ -o fermat.cc main.cc
main.cc: In function `int main()':
main.cc:8: error: `Fermat' was not declared in this scope
main.cc:8: error: `f' was not declared in this scope
main.cc:8: error: `Fermat' is not a type

main.cc

#include <iostream>
#include "fermat.h"

using namespace std;

int main() {

        Fermat* f = new Fermat(81);

        cout << "Is the number " << f->getNum() << " prime? ";
        cout << f->isPrime() << endl;

        return 0;
}

fermat.h

#define FERMATH
#ifndef FERMATH

#include <iostream>
#include <cmath>

using namespace std;

class Fermat {
        private:
                long prime p;
        public:
                Fermat();
                Fermat(long num);
                long getNum();
                bool isPrime();
}

#endif

fermat.cc

#define FERMATCC
#ifndef FERMATCC

#include "fermat.h"
#include <iostream>
#include <cmath>

Fermat::Fermat() {
        // Do Nothing
}

Fermat::Fermat(long num) {
        p = num;
}

bool Fermat::isPrime() {

        // Will redo later.  Probably doesn't work.
        // Will also error if number is less than 10.

        int a = p;
        int pm1 = p - 1;
        bool allPrime = true;
        for(int i=0; i<10; i++) {
                a--;
                if(pow(a, pm1) % p != 1) {
                        allPrime = false;
                }
        }

        return allPrime;
}

long Fermat::getNum() {
        return p;
}

#endif

Edited by bletchley: n/a

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by nezachem
1

In java you do not need put a semicolon after the class deceleration. In
C++ you need it. So this part :

#define FERMATH
#ifndef FERMATH

#include <iostream>
#include <cmath>

using namespace std;

class Fermat {
        private:
                long prime p;
        public:
                Fermat();
                Fermat(long num);
                long getNum();
                bool isPrime();
};
#endif
0

Thank you for the reply. I never would have thought of that. I am still getting the same error message though. Thoughts?

main.cc: In function âint main()â:
main.cc:8: error: âFermatâ was not declared in this scope
main.cc:8: error: âfâ was not declared in this scope
main.cc:8: error: expected type-specifier before âFermatâ
main.cc:8: error: expected `;' before âFermatâ
make: *** [main.o] Error 1

Edited by bletchley: n/a

0
#define FERMATH
#if[B]n[/B]def FERMATH

class Fermat {...}

Your include guards are wrong. What happens here is that the code for class Fermat is only included if FERMATH is not defined (and of course it is!). Swap the directives at the beginning of the file.

Edited by nezachem: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.