C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 8th, 2009, 04:17 PM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #2  
Old March 9th, 2009, 03:08 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
C++ is compiled using a one-pass 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 C-macros for TRUE and FALSE
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
  #3  
Old March 9th, 2009, 03:45 AM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #4  
Old March 9th, 2009, 03:51 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
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

Reply With Quote
  #5  
Old March 9th, 2009, 04:02 AM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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$

Reply With Quote
  #6  
Old March 9th, 2009, 04:06 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
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.

Reply With Quote
  #7  
Old March 9th, 2009, 03:54 PM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #8  
Old March 9th, 2009, 07:54 PM
monster monster is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 127 monster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old March 9th, 2009, 08:19 PM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old March 9th, 2009, 09:37 PM
monster monster is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 127 monster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #11  
Old March 9th, 2009, 10:20 PM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #12  
Old March 9th, 2009, 11:36 PM
monster monster is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 127 monster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #13  
Old March 9th, 2009, 11:43 PM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #14  
Old March 10th, 2009, 12:15 AM
monster monster is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 127 monster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #15  
Old March 10th, 2009, 02:42 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Try this, I only slightly changed the code.
cpp Code:
Original - cpp Code
  1. bool isPrime(int number)
  2. {
  3.    int count;
  4.    bool isprime = true;
  5.  
  6.    /* Every even number is not prime , except 2*/
  7.    if (number == 2) return true;
  8.    if (number % 2 == 0) return false;
  9.  
  10.    /* check every odd number up to the square root of the number */
  11.    double sqrtOfNumber = sqrt( static_cast<double>(number) );
  12.    int s = static_cast<int>( floor(sqrtOfNumber) );
  13.    for (count=3; count<=s; count+=2);
  14.    {
  15.       if (number % count == 0)
  16.       isprime = false;
  17.    }
  18.    return isprime;
  19. }

Reply With Quote
  #16  
Old March 10th, 2009, 04:00 AM
Taka Taka is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 33 Taka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #17  
Old March 10th, 2009, 04:52 AM
monster monster is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 127 monster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > "not declared in this scope"


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap