获取:错误 C2668:'sqrt':对重载函数的调用不明确 [英] Getting: error C2668: 'sqrt' : ambiguous call to overloaded function

查看:31
本文介绍了获取:错误 C2668:'sqrt':对重载函数的调用不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从教科书中给出的示例构建以下源代码.我使用的是 Visual Studio 2008.

Trying to build the source code below from an example given in a textbook. I'm using visual studio 2008.

编译器似乎不知道用筛子做什么:

The compiler doesn't seem to know what to do with sieve:

1>------ Rebuild All started: Project: fig21.40, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for 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 to overloaded function
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(581): could be '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 to match the argument list '(const int)'
1>Build log was saved at "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 ==========

代码:

//Fig. 21.40: fig21_40.cpp
//Using a bitset to demonstrate the 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 the range 2 to 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 from user to determine whether value is prime
cout << "\nEnter a value from 1 to 1023(-1 to end): ";
cin>>value;

while(value != -1){
    if(sieve[value])
        cout << value << " is a prime number\n";
    else
        cout << value << " is not a prime number\n";

    cout << "\nEnter a value from 2 to 1023 (-1 to end): ";
    cin >> value;
}//end while



return 0;
}

推荐答案

我认为这是因为在较新版本的 C++ 中,sqrt 被重载(参数可以是 doublefloatlong double) 并传入一个 int.只需将参数转换为 double 以使其清楚:

I think that is because in newer versions of C++, sqrt is overloaded (argument can be double, float or long double) and you pass in an int. Just cast the argument to double to make it clear:

int finalBit = sqrt( (double) (sieve.size()) ) + 1;

这篇关于获取:错误 C2668:'sqrt':对重载函数的调用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆