c++ - Getting: error C2668: 'sqrt' : ambiguous call to overloaded function -
this question has answer here:
trying build source code below example given in textbook. i'm using visual studio 2008.
the compiler doesn't seem know sieve:
1>------ rebuild started: project: fig21.40, configuration: debug win32 ------ 1>deleting intermediate , output files project 'fig21.40', configuration 'debug|win32' 1>compiling... 1>fig21_40.cpp 1>c:\users\ocuk\documents\c++\chapter 21\fig21.40\fig21.40\fig21_40.cpp(27) : error c2668: 'sqrt' : ambiguous call overloaded function 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(581): 'long double sqrt(long double)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(533): or 'float sqrt(float)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(128): or 'double sqrt(double)' 1> while trying match argument list '(const int)' 1>build log saved @ "file://c:\users\ocuk\documents\c++\chapter 21\fig21.40\fig21.40\debug\buildlog.htm" 1>fig21.40 - 1 error(s), 0 warning(s) ========== rebuild all: 0 succeeded, 1 failed, 0 skipped ==========
code:
//fig. 21.40: fig21_40.cpp //using bitset demonstrate sieve of eratosthenses #include <iostream> using std::cin; using std::cout; using std::endl; #include <iomanip> using std::setw; #include <bitset> //bitset class definition #include <cmath> //sqrt prototype int main() { const int size=1024; int value; std::bitset<size> sieve; sieve.flip(); //perform sieve of eratosthenses int finalbit = sqrt( sieve.size() ) + 1; for(int i=2; i<finalbit; ++i) if(sieve.test(i)) for(int j=2*i; j<size; j+=i) sieve.reset(j); cout << "the prime numbers in range 2 1023 are:\n"; //display prime numbers in range 2-1023 for(int k=2, counter=0; k<size; ++k) if(sieve.test(k)){ cout <<setw(5)<<k; if(++counter % 12 ==0) cout << '\n'; }//end outer if cout << endl; //get value user determine whether value prime cout << "\nenter value 1 1023(-1 end): "; cin>>value; while(value != -1){ if(sieve[value]) cout << value << " prime number\n"; else cout << value << " not prime number\n"; cout << "\nenter value 2 1023 (-1 end): "; cin >> value; }//end while return 0; }
i think because in newer versions of c++
, sqrt
overloaded (argument can double
, float
or long double
) , pass in int. cast argument double
make clear:
int finalbit = sqrt( (double) (sieve.size()) ) + 1;
Comments
Post a Comment