程序让生活更美好

半亩方塘 天光云影

  C++博客 ::  :: 新随笔 :: 联系 ::  :: 管理 ::
  55 随笔 :: 4 文章 :: 202 评论 :: 0 Trackbacks

Dev-c++和VC2005中编译同一个程序出现的问题

   有两段程序,其实应该是一段程序,只是稍微有点不同。程序

的主要目的很简单,就是要输出1-25的平方根和平方。


   第一段在Dev-C++中编译通过的,就叫程序一:

 1#include <iostream>
 2#include <math.h>
 3using namespace std;
 4int 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>
 3using namespace std;
 4int 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函数中的参数类型。

 
  现象:
         程序一在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。
    
posted on 2005-12-30 18:22 北风之神007 阅读(6165) 评论(6)  编辑 收藏 引用 所属分类: c/c++Tools

评论

# re: Dev-c++和VC2005中编译同一个程序出现的问题 2006-01-05 12:07 梦在天涯
微软的2005对目前c++标注好多的都不支持!

增加了安全机制!  回复  更多评论
  

# re: Dev-c++和VC2005中编译同一个程序出现的问题 2006-01-08 00:19 斑竹
我看VC++ 2005非常优秀,我痕喜欢
你把for(int i=1;i<=25;i++){
定义为float就可以了  回复  更多评论
  

# re: Dev-c++和VC2005中编译同一个程序出现的问题 2006-01-20 13:34 郑磊
终于找到大侠了,我也下了一个2005,用起来和6.0差距挺大的,请大哥教我两招吧,谢谢啦。。。。。。。。。。QQ:504831139 E-MAIL:zhengleifrank200@163.com  回复  更多评论
  

# re: Dev-c++和VC2005中编译同一个程序出现的问题 2006-01-24 16:11 noar
用vc2005的ide就行了,不用其余的功能  回复  更多评论
  

# re: Dev-c++和VC2005中编译同一个程序出现的问题 2006-03-28 16:10 ujskilor
我有个dev c++的 模板函数调用错误的问题请教!

template <typename Type>
inline Type max( Type t1, Type t2 )
{ return t1 > t2 ? t1 : t2; }

template <typename elemType>
inline elemType max( const vector<elemType> &vec )
{ return *max_element( vec.begin(), vec.end() ); }

template <typename arrayType>
inline arrayType max( const arrayType *parray, int size )
{ return *max_element( parray, parray+size ); }
上述是我在头文件中声明的模板函数
在cpp文件中调用,出现错误
note D:\ACpp\moban\mb.h:15 candidates are: Type max(Type, Type) [with Type = int]

但是在vc 和bcb中良好。。  回复  更多评论
  

# re: Dev-c++和VC2005中编译同一个程序出现的问题 2008-09-02 19:49 trim
重载解析错误,对sqrt(int, int)的解析过程中,出现多义性。

int可能隐式转换为float, double, long double.

float sqrt(float);
double sqrt(double);
long double sqrt(long double);

vc2005等支持标准c++时,可能要等微软把C++标准委员会买了的时候吧。 哈哈  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理