LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1196|回复: 12

简单的C++问题.

[复制链接]
发表于 2003-8-26 16:41:41 | 显示全部楼层 |阅读模式
下面是source code:

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.    void y(int i)
  6.    {
  7.       cout<<"the value of i";
  8.       cout<<i;
  9.       cout<<'\n';
  10.    }
  11. }
复制代码

下面是error information:
[root@localhost 0308]# g++ cout.C -o cout
cout.C: In function `int main()':
cout.C:6: parse error before `{' token
cout.C:8: `i' undeclared (first use this function)
cout.C:8: (Each undeclared identifier is reported only once for each function
   it appears in.)
cout.C: At global scope:
cout.C:11: parse error before `}' token

烦指点一下.
Thank you.
发表于 2003-8-26 17:36:37 | 显示全部楼层
函数中不能定义函数的。
你把void y(int i)定义在int main()中了。
 楼主| 发表于 2003-8-26 20:36:22 | 显示全部楼层
Thank you.
Now, 编译成功.
But, 结果不太好,
Here is my program that is modified.

  1. #include<iostream>
  2. #include<istream>
  3. using namespace std;
  4. int main()
  5. {
  6.    cout<<"thank you run the program.\n";
  7. }
  8. void y(int i)
  9. {
  10.    cin>>i;
  11.    cout<<"the value of i";
  12.    cout<<i;
  13.    cout<<'\n';
  14. }
复制代码


[root@localhost 0308]# g++ function.C -o function
[root@localhost 0308]# ./function
thank you run the program.
[root@localhost 0308]#

运行时,程序并没有等待用户输入,
也就是说,函数void y(int i)部分仍有错误.
烦指点一下.
Thank you.
发表于 2003-8-26 20:45:11 | 显示全部楼层

  1. #include<iostream>
  2. #include<istream>
  3. using namespace std;
  4. int main()
  5. {
  6.    y();
  7.    cout<<"thank you run the program.\n";
  8. }
  9. void y()
  10. {
  11.    int i;
  12.    cin>>i;
  13.    cout<<"the value of i";
  14.    cout<<i;
  15.    cout<<'\n';
  16. }

复制代码

C++中程序都是从main函数开始的,所有的函数要被main直接或者间接的调用才会运行的。
 楼主| 发表于 2003-8-26 21:03:40 | 显示全部楼层
Thank you.
我使用了你修改后的代码,
下面是error information:

[root@localhost 0308]# g++ function.C -o function
function.C: In function `int main()':
function.C:6: `y' undeclared (first use this function)
function.C:6: (Each undeclared identifier is reported only once for each
   function it appears in.)
function.C: In function `void y()':
function.C:10: `void y()' used prior to declaration
[root@localhost 0308]#

又不能正常编译了.
烦指点一下.
Thank you.
发表于 2003-8-26 21:18:46 | 显示全部楼层
Sorry.一时疏忽,忘了声明函数

  1. #include<iostream>
  2. #include<istream>
  3. using namespace std;
  4. void y();

  5. int main()
  6. {
  7.    y();
  8.    cout<<"thank you run the program.\n";
  9. }
  10. void y()
  11. {
  12.    int i;
  13.    cin>>i;
  14.    cout<<"the value of i";
  15.    cout<<i;
  16.    cout<<'\n';
  17. }

复制代码

这回应该可以了
 楼主| 发表于 2003-8-26 22:12:56 | 显示全部楼层
Excellent helps!

I start using c++ by reading The C++ Programming Language,
third edition by Bjarne Stroustrup.
I have read chapter 1, and chapter 2, the problem I met is in chapter 3,
It seems that Bjarne Stroustrup doesn't explain that a function can not be defined
within another function, all of the fuctions except int main() must be called within the
function int main(), how to call the others within the function int main(),
Most of the source codes in the book <<the c++ programming language>> is not complete.

I have met the problem for two days, have had a look at <<c++ primer>> and
<<thinking in c++>> not carefully to try to fix the problem that I mentioned in my
previous posts, and have searched the resources in web, but all falled.

You posts are of beautiful helps to me.
I am in Linux now, it doesn't has Wubi inputing application.
 楼主| 发表于 2003-8-26 22:31:12 | 显示全部楼层
请问, 如果函数y的定义语句仍是void y(int i) 而不是void y(),
又应怎么修改代码?
Thank you.
发表于 2003-8-26 22:50:17 | 显示全部楼层
调用一个函数:

  1. void y(int); //要有函数原型

  2. int
  3. main(void)
  4. {
  5.   y(i); //调用
  6. }

  7. void
  8. y(int i) //函数定义
  9. {
  10. }
复制代码

其它代码省略
发表于 2003-8-26 22:51:18 | 显示全部楼层
函数的参数跟返回值是调用者跟被调用者交换信息用的。
参数由调用者传给被调用者;
返回值有被调用者传给调用者。
在你这个例子中,由于main不需要传递信息给y,y也不需要传递信息给main,所以我就选择了void y(void)这个函数形式。当然你如果非要用void y(int)也行,就是有main传个没用的参数给y.
或者改成这样:

  1. #include<iostream>
  2. #include<istream>
  3. using namespace std;
  4. void y(int i);

  5. int main()
  6. {
  7.    int i;
  8.    cin >> i;
  9.    y(i);
  10.    cout<<"thank you run the program.\n";
  11. }
  12. void y(int i) //output variable i.
  13. {
  14.    cout<<"the value of i";
  15.    cout<<i;
  16.    cout<<'\n';
  17. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表