一个极简单的C++程序竟然在Visual C++ 6.0中编译报错, 请教各位高手是咋回事?

szwind 2008-03-16 01:24:35
#include <math.h>
int test(int x, int y)
{
return max(0.0, 2.1);
}

=====================================================================================
以上一个及其简单的程序竟然在Visual C++ 6.0中编译报错:

--------------------Configuration: OptionPricing - Win32 Debug--------------------
Compiling...
test.cpp
C:\SAMPLE CODE\OptionPricing\test.cpp(4) : error C2065: 'max' : undeclared identifier
Error executing cl.exe.

test.obj - 1 error(s), 0 warning(s)
请教各位高手是咋回事?
...全文
203 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
scq2099yt 2008-03-16
  • 打赏
  • 举报
回复
支持1楼
coolcoffee4051982 2008-03-16
  • 打赏
  • 举报
回复
max没定义,不知道用的什么版本的VC
zhengjinye 2008-03-16
  • 打赏
  • 举报
回复
//#include <math.h>
double test(int x, int y)
{
return max(x, y);
}
以上代码在VS2008上测试通过,不用#include <math.h>就可以。
sunlin7 2008-03-16
  • 打赏
  • 举报
回复
#include <windows.h>
int main()
{
printf("%d", MAX(1,2));
return 0;
}
保证通过.
sunlin7 2008-03-16
  • 打赏
  • 举报
回复
MAX(1,2);
是大写的,c/c++大小写敏感的,晕
野男孩 2008-03-16
  • 打赏
  • 举报
回复
6楼正解,就这小需求还用stl,大材小用。

vc6我看就挺好用的~占的资源少,需求基本都能满足~
arong1234 2008-03-16
  • 打赏
  • 举报
回复
VC6人家微软都不支持了:)
换2005或者2008吧
arong1234 2008-03-16
  • 打赏
  • 举报
回复
在如下头文件中都又定义
stdlib.h
minmax.h
windef.h
VC6没有在algorithm中定义,它本来就是不很标准,因为它比C++标准出来的更早
yuntianhai 2008-03-16
  • 打赏
  • 举报
回复
#define max(a,b) ((a)> (b)?(a):(b))

写成这样的 这样就没有问题的
szwind 2008-03-16
  • 打赏
  • 举报
回复
楼上的兄弟呀, 加了using namespace std; 还是不行呀。
#include <algorithm>
using namespace std;
double test(int x, int y)
{
return max(0.0, 2.1);
}

====================================================================
--------------------Configuration: OptionPricing - Win32 Debug--------------------
Compiling...
test.cpp
C:\SAMPLE CODE\OptionPricing\test.cpp(7) : error C2065: 'max' : undeclared identifier
Error executing cl.exe.

test.obj - 1 error(s), 0 warning(s)
菜牛 2008-03-16
  • 打赏
  • 举报
回复
标准函数库没有这个函数;如果用STL,要加namespace,比如:
// valarray_max.cpp
// compile with: /EHsc
#include <valarray>
#include <iostream>

int main( )
{
using namespace std;
int i, MaxValue;

valarray<int> vaR ( 10 );
for ( i = 0 ; i < 10 ; i += 3 )
vaR [ i ] = i;
for ( i = 1 ; i < 10 ; i += 3 )
vaR [ i ] = 2*i - 1;
for ( i = 2 ; i < 10 ; i += 3 )
vaR [ i ] = 10 - i;

cout << "The operand valarray is: ( ";
for (i = 0 ; i < 10 ; i++ )
cout << vaR [ i ] << " ";
cout << ")." << endl;

MaxValue = vaR.max ( );
cout << "The largest element in the valarray is: "
<< MaxValue << "." << endl;
}
szwind 2008-03-16
  • 打赏
  • 举报
回复
可是, 就算换成:
#include <algorithm>
double test(int x, int y)
{
return max(0.0, 2.1);
}

还是不行呀。

--------------------Configuration: OptionPricing - Win32 Debug--------------------
Compiling...
test.cpp
C:\SAMPLE CODE\OptionPricing\test.cpp(6) : error C2065: 'max' : undeclared identifier
Error executing cl.exe.

test.obj - 1 error(s), 0 warning(s)
superdiablo 2008-03-16
  • 打赏
  • 举报
回复
应该是因为max不在math.h中定义。
可以自己写一个:
#define max(a,b) ((a)>(b)?(a):(b))

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧