编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛  
 
全能 ASP / PHP / ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
发新话题
打印

[求助]请帮忙指点

[求助]请帮忙指点

这是我第一次用c++写的程序

编译时说有12个错误,可把我给打击坏了

请帮忙解决一下 大恩不言谢

#include <iostream.h> int & put(int n); int get (int n); int vals[10]; int error=-1; void main() { put(0)=10; put(1)=20; put(9)=30; cout<<get(0)<<endl; cout<<get(1)<<endl; cout<<get(9)<<endl; put(12)=1; } int& put(int n) { if(n>=10) { cout<<"range error in put() value!"<<endl; exit error;} return vals[n]; }

int get(int n) {if(n>=10) { cout<<"range error in get() value!"<<endl; exit error;} return vals[n]; }

下面是错误:

D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(2) : error C2059: syntax error : '&' D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(7) : warning C4013: 'put' undefined; assuming extern returning int D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(7) : error C2106: '=' : left operand must be l-value D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(8) : error C2106: '=' : left operand must be l-value D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(9) : error C2106: '=' : left operand must be l-value D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(10) : error C2065: 'cout' : undeclared identifier D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(10) : error C2297: '<<' : illegal, right operand has type 'char [3]' D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(11) : error C2065: 'endl' : undeclared identifier D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(11) : warning C4552: '<<' : operator has no effect; expected operator with side-effect D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(12) : warning C4552: '<<' : operator has no effect; expected operator with side-effect D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(13) : warning C4552: '<<' : operator has no effect; expected operator with side-effect D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(14) : error C2106: '=' : left operand must be l-value D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(16) : error C2059: syntax error : '&' D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(25) : error C2297: '<<' : illegal, right operand has type 'char [28]' D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(26) : error C2065: 'exit' : undeclared identifier D:\Program Files\Microsoft Visual Studio\Vc98\yinyong.c(26) : error C2146: syntax error : missing ';' before identifier 'error' Error executing cl.exe.

yinyong.obj - 12 error(s), 4 warning(s)

TOP

修改如下:

#include[/COLOR] <iostream> //使用标准C++,去掉后面的.h[/COLOR] using[/COLOR] namespace[/COLOR] std; //加上这句[/COLOR] int[/COLOR] & put(int[/COLOR] n); int[/COLOR] get (int[/COLOR] n); int[/COLOR] vals[10]; //int error =-1;[/COLOR] int[/COLOR] main[/COLOR]() //返回值改成int [/COLOR] { put( 0 )=10; put( 1 )=20; put( 9 )=30; cout << get( 0 ) << endl; cout << get( 1 ) << endl; cout << get( 9 ) << endl; put( 12 )=1; getchar(); //暂停关闭运行窗口[/COLOR] return[/COLOR] 0; } int[/COLOR]& put(int[/COLOR] n) { if[/COLOR](n>=10) { cout << "range error in put() value!"[/COLOR] << endl; getchar(); //加暂停,否则什么也看不见 [/COLOR] exit (1); } return[/COLOR] vals[n]; }

int[/COLOR] get(int[/COLOR] n) { if[/COLOR](n>=10) { cout << "range error in get() value!"[/COLOR] << endl; getchar(); //加暂停[/COLOR] exit (1); getchar(); } return[/COLOR] vals[n]; }

[此贴子已经被作者于2004-12-10 14:57:08编辑过]

TOP

这样写的话,要是我传进去的i是负的,不是一样会出错?
http://www.allaboutprogram.com/ http://www.polyrandom.com/

TOP

多谢二位的指点

还请以后多多指教!!

TOP

using namespace std; //加上这句

为什么要加上这句?

TOP

引用:
using namespace std; //加上这句

为什么要加上这句?

不加这句就得用:

#include <iostream.h> //后面加.h

TOP

可我就是用了

#include <iostream.h>

执行时出了一大堆错

用using namesapce std; 就没错

着是怎么回事?

是不是编译器不一样

我用的是VC++6,0

我每次用#include <iostream.h>都不行

TOP

这样就可以了! #include <iostream.h> #include <stdlib.h> int & put(int n); int get (int n); int vals[10]; const int ERROR=-1; void main() { put(0)=10; put(1)=20; put(9)=30; cout<<get(0)<<endl; cout<<get(1)<<endl; cout<<get(9)<<endl; put(12)=1; } int& put(int n) { if(n>=10) { cout<<"range error in put() value!"<<endl; exit(ERROR);} return vals[n]; }

int get(int n) {if(n>=10) { cout<<"range error in get() value!"<<endl; exit(ERROR);} return vals[n]; }

[此贴子已经被作者于2004-12-29 13:38:35编辑过]

天自潇洒随己意,人又何故负今生!

TOP

发新话题