请大家帮我看看这个简单的c++程序为什么编译错误!

软件和网站开发以及相关技术探讨
回复
benben
帖子: 15
注册时间: 2006-03-20 9:44

请大家帮我看看这个简单的c++程序为什么编译错误!

#1

帖子 benben » 2006-09-13 22:05

gcc 版本 4.0.3 (Ubuntu 4.0.3-1ubuntu5)


程序源码:

//8-9.cpp
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;

int main()
{
float x=1.0;
float &y=x;
float *p=&x;

cout << "The original value of x is:" << x << endl;
*p=5.0;
cout << x << endl;
y=7.3;
cout << x << endl;
}


编译命令:g++ 8-9.cpp -o 8-9

编译输出信息:

8-9.cpp:2: 错误: ‘std::cin’ 尚未声明
8-9.cpp:3: 错误: ‘std::cout’ 尚未声明
8-9.cpp: In function ‘int main()’:
8-9.cpp:12: 错误: ‘cout’ 在此作用域中尚未声明
头像
nobrain
帖子: 808
注册时间: 2005-08-25 13:58
来自: ustc
联系:

#2

帖子 nobrain » 2006-09-13 22:21

我好久没用c++了,但是

代码: 全选

using std::cin;
using std::cout;
using std::endl; 
似乎应该写成

代码: 全选

using namespace std;
至少你那种写法我从来没见过。
爱喝真猪奶茶的夜鸣猪
benben
帖子: 15
注册时间: 2006-03-20 9:44

改过了,还是不行,好奇怪,到底是为什么呢?

#3

帖子 benben » 2006-09-14 7:22

按照nobrain的说法改过之后还是不行!


源码是:

#include <iomanip>

using namespace std;

int main()
{
float x=1.0;
float &y=x;
float *p=&x;

cout << "The original value of x is:" << x << endl;
*p=5.0;
cout << "*p=5.0:\n" << x << endl;
y=7.3;
cout << "y=7.3:\n" << x << endl;
}


提示信息是:

8-9.cpp: In function ‘int main()’:
8-9.cpp:11: 错误: ‘cout’ 在此作用域中尚未声明
头像
5451vs5451
帖子: 345
注册时间: 2006-07-14 18:56
来自: Apple Valley, Planet Tux, Linux System

#4

帖子 5451vs5451 » 2006-09-14 8:40

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
可以这么用的。
benben
帖子: 15
注册时间: 2006-03-20 9:44

#5

帖子 benben » 2006-09-14 9:26

两种方式都出错,是不是跟g++版本或者设置有关?
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#6

帖子 BigSnake.NET » 2006-09-14 13:02

#include <iostream>
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
benben
帖子: 15
注册时间: 2006-03-20 9:44

问题终于解决啦!

#7

帖子 benben » 2006-09-14 16:31

把using namespace std;或者using std::cin; using std::cout; using std::endl; 放在main()体内就可以了!
baobaobenben
帖子: 3
注册时间: 2006-08-25 10:10

奇怪

#8

帖子 baobaobenben » 2006-09-14 17:20

楼上的 方法 都 我 都 试了下。
都有问题。。。。cout 始终是为申明状态。。。。。
#include <iostream>

int main()
{
float x=1.0;
float &y=x;
float *p=&x;

cout << "The original value of x is:" << x << endl;
*p=5.0;
cout << x << endl;
y=7.3;
cout << x << endl;
}

这样就 OK拉
benben
帖子: 15
注册时间: 2006-03-20 9:44

#9

帖子 benben » 2006-09-14 18:56

baobaobenben说的对!
但是为什么不能用头文件iomanip呢!
用google搜索iomanip的用法(http://www.fredosaurus.com/notes-cpp/io ... ators.html):
1. using namespace std;
2. using std::cin;
using std::cout;
using std::endl;
这里为什么就不可以呢?!
头像
5451vs5451
帖子: 345
注册时间: 2006-07-14 18:56
来自: Apple Valley, Planet Tux, Linux System

#10

帖子 5451vs5451 » 2006-09-14 22:56

不是不可以,如果你要用I/O Manipulator当然要#include <iomanip>,但是cin和cout不是I/O Manipulator,要用它们就得#include <iostream>。

BTW:baobaobenben和benben不是同一个人吗?我还以为是马甲。
benben
帖子: 15
注册时间: 2006-03-20 9:44

#11

帖子 benben » 2006-09-15 20:35

感谢5451vs5451!问题终于解决啦!
声明:benben和baobaobenben不是一个人,哈哈!
头像
feeling
帖子: 175
注册时间: 2006-04-29 20:10
来自: 北京·昌平
联系:

#12

帖子 feeling » 2006-10-27 9:38

标准情况下,std名空间应该是在头文件iostream里面声明的。
某些编译器可能仅仅使用头文件iomanip也可以,但不是所有的情况下都管用。

还是用标准用法吧,移植起来方便。

另外,关于std的用法,如果用到很多该名空间里面的东东,那就

代码: 全选

using namespace std;
如果只是简单的几个,可以只将这几个东东声明一下即可,如:

代码: 全选

using std::cin;
using std::cout;
using std::cerr;
人生若只如初见,何事秋风悲画扇?
illidan
帖子: 14
注册时间: 2006-10-01 23:48

Re: 请大家帮我看看这个简单的c++程序为什么编译错误!

#13

帖子 illidan » 2006-11-09 15:39

使用using directive可以访问namespace里的所有成员。使用using declaration可以访问namespace的某个成员。

前者的语法是using namespace std;

后者的语法是using std::cin;

如果在函数里使用这两个语句,则它们的作用域限于该函数。

在你的例子中,你使用using declaration,可是std::in是什么,std::out是什么?这在<iostream>里定义,所以得#include <iostream>

<iomanip>是关于一些I/O配置、操作的,比如cout << setw(5);
benben 写了:gcc 版本 4.0.3 (Ubuntu 4.0.3-1ubuntu5)


程序源码:

//8-9.cpp
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;

int main()
{
float x=1.0;
float &y=x;
float *p=&x;

cout << "The original value of x is:" << x << endl;
*p=5.0;
cout << x << endl;
y=7.3;
cout << x << endl;
}


编译命令:g++ 8-9.cpp -o 8-9

编译输出信息:

8-9.cpp:2: 错误: ‘std::cin’ 尚未声明
8-9.cpp:3: 错误: ‘std::cout’ 尚未声明
8-9.cpp: In function ‘int main()’:
8-9.cpp:12: 错误: ‘cout’ 在此作用域中尚未声明
myforever
帖子: 3
注册时间: 2006-11-13 21:53

#14

帖子 myforever » 2006-11-15 22:52

分别引用using std::cin; using std::cout; using std::endl;显得似乎麻烦,直接using namespace std;全包含进去就行了嘛。
chl2090
帖子: 23
注册时间: 2006-10-26 21:11

#15

帖子 chl2090 » 2006-11-23 15:11

main有返回值,应该在后面加个return 0;
回复