Thread: Not declared in this scope

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Not declared in this scope

    Hey if anyone could help me out here I would really appreciate it. I'm not sure what do to to get rid of the below errors/warnings.

    prime.cpp: In function 'int main()':
    prime.cpp:12: error: 'isPrime' was not declared in this scope
    prime.cpp: In function 'int isPrime(int)':
    prime.cpp:39: warning: converting to 'int' from 'double'

    Code:
    #include <iostream>
    using namespace std;
    #include <math.h>
     
    #define TRUE 1;
    #define FALSE 0;
     
    int main()
    {
       int number;
     
       if (isPrime(number)) 
          cout << "\n" << number << "is a prime number\n";
       else 
          cout << "\n" << number << "is not a prime number\n";
     
       return 0;
    }
     
    void getNumber(int &number)
    {
       cout << "Please enter a positive number ";
       cin >> number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
     
    int isPrime(int number)
    {
       int count, s;
     
       /* Every even number is not prime */
       if (number % 2 == 0) return TRUE;
     
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0) return TRUE;
       }
       return FALSE;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The compiler doesn't know that "isPrime" is defined later in the file. You need to declare it before using it. Add the following before main:

    int isPrime(int);

    The compiler will then know that "isPrime" is a function taking/returning int, when it sees it.

    gg

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I tried that but it came up with an "unexpected initialiser before int" error.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Sorry I know what it was I was doing wrong.

    Can anyone help me with why it is always printing out:

    Please enter a positive number 4

    -4195180is a prime number

    Updated code:

    Code:
    #include <iostream>
    using namespace std;
    #include <math.h>
    
    int isPrime(int);
    void getNumber(int);
    
    #define TRUE 1;
    #define FALSE 0;
    
    int main()
    {
       int number;
    
       getNumber(number);
    
       if (isPrime(number)) 
          cout << "\n" << number << "is a prime number\n";
       else 
          cout << "\n" << number << "is not a prime number\n";
    
       return 0;
    }
    
    void getNumber(int number)
    {
       cout << "Please enter a positive number ";
       cin >> number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    
    int isPrime(int number)
    {
       int count, s;
    
       /* Every even number is not prime */
       if (number % 2 == 0) return TRUE;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0) return TRUE;
       }
       return FALSE;
    }
    Last edited by Taka; 03-08-2009 at 09:12 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should use pass by reference instead of pass by value, otherwise the number variable in the main function is never modified by getNumber().

    By the way, C++ has a bool type with true and false constants, so it does not make sense to use an int with TRUE and FALSE macros.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I'm sorry but I don't know what you mean at all.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You had it right the first time -- compare the original getNumber with your new one.

    Not only should "#define TRUE 1" never appear in C++, given that "true" already exists, it should most definitely not appear with a semicolon at the end. Since isPrime asks a true-false question, you should use a true-false datatype, specifically "bool".

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    When I left it as

    Code:
     void getNumber(int &number)
    I got a compile error:

    prime.cpp: In function 'int isPrime(int)':
    prime.cpp:44: warning: converting to 'int' from 'double'
    Undefined first referenced
    symbol in file
    getNumber(int) /var/tmp//cc1BaH0f.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you remember to change the prototype too? Otherwise post code.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Code:
    #include <iostream>
    using namespace std;
    #include <math.h>
    
    int isPrime(int);
    void getNumber(int);
    
    #define TRUE 1;
    #define FALSE 0;
    
    int main()
    {
       int number;
    
       getNumber(number);
    
       if (isPrime(number)) 
          cout << "\n" << number << "is a prime number\n";
       else 
          cout << "\n" << number << "is not a prime number\n";
    
       return 0;
    }
    
    void getNumber(int &number)
    {
       cout << "Please enter a positive number ";
       cin >> number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    
    int isPrime(int number)
    {
       int count, s;
    
       /* Every even number is not prime */
       if (number % 2 == 0) return TRUE;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0) return TRUE;
       }
       return FALSE;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, this:
    Code:
    void getNumber(int);
    should be either:
    Code:
    void getNumber(int& number);
    or:
    Code:
    int getNumber();
    Then you make the change in the function definition (implementation) as well.

    Now this:
    Code:
    int isPrime(int);
    should be:
    Code:
    bool isPrime(int number);
    then you get rid of these lines:
    Code:
    #define TRUE 1;
    #define FALSE 0;
    Note that if you really did need them, those semi-colons should be removed, as mentioned by tabstop.

    So, no more TRUE or FALSE. What to do? Just use true or false.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    So it should be like this?

    Code:
    #include <iostream>
    using namespace std;
    #include <math.h>
    
    int isPrime(int);
    void getNumber(int& number);
    
    #define TRUE 1
    #define FALSE 0
    
    int main()
    {
       int number;
    
       getNumber(number);
    
       if (isPrime(number)) 
          cout << "\n" << number << "is a prime number\n";
       else 
          cout << "\n" << number << "is not a prime number\n";
    
       return 0;
    }
    
    void getNumber(int& number)
    {
       cout << "Please enter a positive number ";
       cin >> number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
    }
    
    int isPrime(int number)
    {
       int count, s;
    
       /* Every even number is not prime */
       if (number % 2 == 0) return TRUE;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0) return TRUE;
       }
       return FALSE;
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Taka
    So it should be like this?
    You're not paying attention, are you? The code that you corrected should be removed. I was merely pointing out how it would be corrected if it were not to be removed.

    There is another thing that I did not spot earlier: you have a using directive (using namespace std; ) before a header inclusion. That is bad practice, although it might not matter especially since the header is a standard header. Move the using directive to after the header inclusion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    I'm sorry I'm trying to understand what you are saying but I am only learning C++ and I'm not really getting what you are trying to tell me.

    I've been told I need to leave the True/False statements in there thats why I didn't take them out.

  15. #15
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    bool isPrime(int);
    int getNumber();
    
    int main()
    {
       int number = getNumber();
    
       if (isPrime(number)) 
          cout << "\n" << number << "is a prime number\n";
       else 
          cout << "\n" << number << "is not a prime number\n";
    
       return 0;
    }
    
    int getNumber()
    {
       int number;
       cout << "Please enter a positive number ";
       cin >> number;
       if (!cin.good())
       {
          printf("Invalid number entered\n");
          exit(1);
       }
       return number;
    }
    
    bool isPrime(int number)
    {
       int count, s;
       bool isprime = true;
    
       /* Every even number is not prime */
       // if (number % 2 == 0) return TRUE;
    
       /* check every odd number up to the square root of the number */
       s = sqrt(number);
       for (count=3; count<=s; count+=2);
       {
          if (number % count == 0)
            isprime = false;
       }
       return false;
    }
    Include "cmath" , not "math.h".

    In main(), affect 'number' by returning the number. This is the most logical.
    In the function 'isPrime' you don't have to verify for both 'true' and 'false', just make it true unless otherwise found.
    Try not to use 'return' in loops or functions, but affect a local variable which you return always in the end (although here it doesn't really matter).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM