Dev-c++和VC2005中編譯同一個程序出現的問題
有兩段程序,其實應該是一段程序,只是稍微有點不同。程序
的主要目的很簡單,就是要輸出1-25的平方根和平方。
第一段在Dev-C++中編譯通過的,就叫程序一:
1
#include <iostream>
2
#include <math.h>
3
using namespace std;
4
int main()
{
5
cout<<"N 平方根 平方"<<endl;
6
for(int i=1;i<=25;i++)
{
7
cout<<i<<"\t"<<sqrt(i)<<"\t"<<pow(i,2)<<endl;
8
}
9
getchar();
10
return 0;
11
}
12
#include <iostream>2
#include <math.h>3
using namespace std;4

int main()
{5
cout<<"N 平方根 平方"<<endl;6

for(int i=1;i<=25;i++)
{7
cout<<i<<"\t"<<sqrt(i)<<"\t"<<pow(i,2)<<endl;8
}9
getchar();10
return 0;11
}12

第二段在VC2005中編譯通過的,就叫程序二:
1
#include <iostream>
2
#include <math.h>
3
using namespace std;
4
int main()
{
5
cout<<"N 平方根 平方"<<endl;
6
for(int i=1;i<=25;i++)
{
7
cout<<i<<"\t"<<sqrt((double)i)<<"\t\t"<<pow((double)i,2)<<endl;
8
}
9
getchar();
10
return 0;
11
}
12
兩段程序的主要區別就是sqrt和pow函數中的參數類型。
#include <iostream>2
#include <math.h>3
using namespace std;4

int main()
{5
cout<<"N 平方根 平方"<<endl;6

for(int i=1;i<=25;i++)
{7
cout<<i<<"\t"<<sqrt((double)i)<<"\t\t"<<pow((double)i,2)<<endl;8
}9
getchar();10
return 0;11
}12

現象:
程序一在Dev-C++中可以輕易編譯通過,程序二在Dev-C++中也可以輕易編譯通過。
程序一在VC2005中無法編譯通過,程序二是可以的,程序一在VC2005中編譯的時候會提示以下錯誤。
錯誤如下:
Error 1 error C2668: 'sqrt' : ambiguous call to overloaded function e:\C\vc2005\2\p7\p7\3.cpp 7
Error 2 error C2668: 'pow' : ambiguous call to overloaded function e:\C\vc2005\2\p7\p7\3.cpp 7
在Dev-C++中的math.h中,這兩個數學函數的原型是
_CRTIMP double __cdecl pow (double, double);
_CRTIMP double __cdecl sqrt (double);
在VC2005中的math.h中,這兩個數學函數的原型是
double __cdecl pow(__in double _X, __in double _Y);
double __cdecl sqrt ((__in double _X);
其中多出來的__in的介紹如下:
If you examine the library header files, you will notice some unusual annotations such as __in_z and __out_ecount_part. These are examples of Microsoft's standard source code annotation language (SAL), which provides a set of annotations to describe how a function uses its parameters—the assumptions it makes about them, and the guarantees it makes upon finishing. The header file <sal.h> defines the annotations.
具體的可以看http://msdn2.microsoft.com/en-us/library/ms235402.aspx
函數的原型都是差不多的,參數類型也是一樣。int類型賦值給double應該是沒有問題的,會進行隱式轉換,不知道VC2005怎么不行?一直都聽說Dev-C++對C++的標準支持的很不錯,微軟的最新的C++開發工具在支持C++標準方面也取得了突飛猛進的進步,現在一個程序,在不同地方卻不能同時編譯通過,我不知道是不是哪個對標準的支持有什么問題,還是編譯器提供的安全性不同的原因呢?疑惑ing。

