


Dev Articles Community Forums
> Programming
> C/C++ Help

"not declared in this scope"
Discuss "not declared in this scope" in the C/C++ Help forum on Dev Articles. "not declared in this scope" C/C++ Help forum discussing building and maintaining applications in C/C++. Find out why these languages are the foundation on which other languages are built.






Dev Articles Community Forums Sponsor:


March 8th, 2009, 05:17 PM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


"not declared in this scope"
Hi,
I'm new to C++ and I was wondering if anyone could help me please. When trying to compile the below code I get this error. I've played around with a few things but just wind up with different variations of the same error. Any help would be appreciated. Thanks.
g++ lm prime.cpp
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;
}

March 9th, 2009, 04:08 AM


Command Line Warrior


Join Date: Sep 2005
Posts: 1,021
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15


C++ is compiled using a onepass compiler. Which means a symbol should be declared before using it (class members are the exception..). So you can either put the functions before the function that needs them, i.e., put isPrime() before main().
Or you can make a forward declaration. This means you would leave the code as is but put a single line above main():
int isPrime(int number);
p.s. c++ has a bool type, no need for those Cmacros for TRUE and FALSE
__________________
There is no such thing as C/C++, you either program C or C++

March 9th, 2009, 04:45 AM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


Thanks... I've been playing around with the code trying to get it to work as below. I'm still having compiling issues. If you could have a look at the below for me it would be great. Is that how I should be applying the bool?
Code:
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime(int);
void getNumber(int& number);
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);
}
}
bool 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;
}

March 9th, 2009, 04:51 AM


Command Line Warrior


Join Date: Sep 2005
Posts: 1,021
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15


Instead of TRUE and FALSE, use true and false. I get two errors when compiling your code:
error C2556: 'bool isPrime(int)' : overloaded function differs only by return type from 'int isPrime(int)'
error C2668: 'sqrt' : ambiguous call to overloaded function
The first one is solved by replacing int with bool in your forward declaration. The second one is trickier. The sqrt function takes a float or double argument. You are passing an int and now the compiler does not know which one to use. Figure out if you need a double or can do with a float. Also note that sqrt does not return an int either so if you assign its result to an int it might get truncated in some way.
Hope this helps

March 9th, 2009, 05:02 AM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


Thanks very much for your help. I'll have a play around with the double/float and see if I can work out which suits best.
But would you happen to know what the fatal error below would be and how I could look at fixing it up?
prime.cpp: In function 'bool isPrime(int)':
prime.cpp:42: warning: converting to 'int' from 'double'
Undefined first referenced
symbol in file
getNumber(int&) /var/tmp//ccy5ogNG.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
charlie$

March 9th, 2009, 05:06 AM


Command Line Warrior


Join Date: Sep 2005
Posts: 1,021
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15


Hmm, don't know.. You get this error during linking right? I suggest you clean everything before compiling again. (Remove the exe ( probably a.out) and all object files (.o or .obj files) ).
Besides that it might be more elegant to have the getNumber function return an int instead of taking a reference parameter.

March 9th, 2009, 04:54 PM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


Ok so I changed the code around to get it to compile. I'm just still having issues with changing it to a double.
Do I need to change it so is double getNumber() as well as isPrime(double)??
Code:
#include <iostream>
#include <math.h>
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;
}

March 9th, 2009, 08:54 PM

Contributing User


Join Date: Mar 2009
Posts: 127
Time spent in forums: 19 h 20 m 59 sec
Reputation Power: 10


Quote:
Originally Posted by Taka
Ok so I changed the code around to get it to compile. I'm just still having issues with changing it to a double.
Do I need to change it so is double getNumber() as well as isPrime(double)??
Code:
#include <iostream>
#include <math.h>
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;
}
c
}

In the isPrime(int number), if in your problem somewhere you need to work with decimals then you will have to change to double. Also looking at your code you have "return false;" at the end, did you mean return isprime?
Also you do not need this line
if (number % 2 == 0) return true;
since isprime is initialized to true you can just return isprime.

March 9th, 2009, 09:19 PM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


How would I change to a double though?
I've tried to change all the int references in the isPrime to double but that causes compile errors.

March 9th, 2009, 10:37 PM

Contributing User


Join Date: Mar 2009
Posts: 127
Time spent in forums: 19 h 20 m 59 sec
Reputation Power: 10


Quote:
Originally Posted by Taka
How would I change to a double though?
I've tried to change all the int references in the isPrime to double but that causes compile errors.

post what you currently have

March 9th, 2009, 11:20 PM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


I am compiling using: g++ lm Wall Werror o prime prime.cpp
With the below result:
cc1plus: warnings being treated as errors
prime.cpp: In function 'bool isPrime(int)':
prime.cpp:44: warning: converting to 'int' from 'double'
Code:
#include <iostream>
#include <math.h>
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 false;
if (number == 2) 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 isprime;
}

March 10th, 2009, 12:36 AM

Contributing User


Join Date: Mar 2009
Posts: 127
Time spent in forums: 19 h 20 m 59 sec
Reputation Power: 10


Quote:
Originally Posted by Taka
I am compiling using: g++ lm Wall Werror o prime prime.cpp
With the below result:
cc1plus: warnings being treated as errors
prime.cpp: In function 'bool isPrime(int)':
prime.cpp:44: warning: converting to 'int' from 'double'
Code:
#include <iostream>
#include <math.h>
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 false;
if (number == 2) 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 isprime;
}

I believe you use of s = sqrt(number); is wrong, sqrt takes a double and you are passing in an int.

March 10th, 2009, 12:43 AM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


What would I use instead of the sqrt though? Because I need the sqrt to determine the stop on the loop.

March 10th, 2009, 01:15 AM

Contributing User


Join Date: Mar 2009
Posts: 127
Time spent in forums: 19 h 20 m 59 sec
Reputation Power: 10


Quote:
Originally Posted by Taka
What would I use instead of the sqrt though? Because I need the sqrt to determine the stop on the loop.

So I took another quick look at your code, are you sure your algorithm is correct? I could be wrong but, looks kinda weird.
eg. 5 is a whole number, sqrt of 5 will not give you a whole number. That is why sqrt has to be stored as double.
and one of your cases if (number == 2) return true;
are you sure you want that there?

March 10th, 2009, 03:42 AM


Command Line Warrior


Join Date: Sep 2005
Posts: 1,021
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 15


Try this, I only slightly changed the code.
cpp Code:
Original
 cpp Code 


bool isPrime(int number) { int count; bool isprime = true; /* Every even number is not prime , except 2*/ if (number == 2) return true; if (number % 2 == 0) return false; /* check every odd number up to the square root of the number */ double sqrtOfNumber = sqrt( static_cast<double>(number) ); int s = static_cast<int>( floor(sqrtOfNumber) ); for (count=3; count<=s; count+=2); { if (number % count == 0) isprime = false; } return isprime; }

March 10th, 2009, 05:00 AM

Contributing User


Join Date: Mar 2009
Posts: 33
Time spent in forums: 4 h 44 m 20 sec
Reputation Power: 10


Thanks guys. I really appreciate the help. I finally worked out that by changing the int s to double s it got rid of the error.
One last question. I've been told that I need to use pointers to get this to run. Can anyone help me change the code so that I'm using pointers? So the below is what I've tried and the compile error that I'm getting.
g++ lm prime.cpp
prime.cpp: In function 'int main()':
prime.cpp:13: error: invalid type argument of 'unary *'
Code:
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime(int);
int getNumber(int);
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);
}
}
bool isPrime(int number)
{
int count;
double s;
bool isprime = true;
/* Every even number is not prime */
if (number == 2) return true;
if (number % 2 == 0) return false;
/* 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 false;
}
return isprime;
}

March 10th, 2009, 05:52 AM

Contributing User


Join Date: Mar 2009
Posts: 127
Time spent in forums: 19 h 20 m 59 sec
Reputation Power: 10


Quote:
Originally Posted by Taka
Thanks guys. I really appreciate the help. I finally worked out that by changing the int s to double s it got rid of the error.
One last question. I've been told that I need to use pointers to get this to run. Can anyone help me change the code so that I'm using pointers? So the below is what I've tried and the compile error that I'm getting.
g++ lm prime.cpp
prime.cpp: In function 'int main()':
prime.cpp:13: error: invalid type argument of 'unary *'
Code:
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime(int);
int getNumber(int);
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);
}
}
bool isPrime(int number)
{
int count;
double s;
bool isprime = true;
/* Every even number is not prime */
if (number == 2) return true;
if (number % 2 == 0) return false;
/* 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 false;
}
return isprime;
}

take out the * getNumber(*number);
int getNumber(int);
match your declaration with your function.

Developer Shed Advertisers and Affiliates
Thread Tools 
Search this Thread 


Display Modes 
Rate This Thread 
Linear Mode


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off




