请大家帮我看看这个简单的c++程序为什么编译错误!
-
- 帖子: 15
- 注册时间: 2006-03-20 9:44
请大家帮我看看这个简单的c++程序为什么编译错误!
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’ 在此作用域中尚未声明
程序源码:
//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
- 联系:
-
- 帖子: 15
- 注册时间: 2006-03-20 9:44
改过了,还是不行,好奇怪,到底是为什么呢?
按照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’ 在此作用域中尚未声明
源码是:
#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
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
-
- 帖子: 3
- 注册时间: 2006-08-25 10:10
奇怪
楼上的 方法 都 我 都 试了下。
都有问题。。。。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拉
都有问题。。。。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拉
-
- 帖子: 15
- 注册时间: 2006-03-20 9:44
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;
这里为什么就不可以呢?!
但是为什么不能用头文件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
- feeling
- 帖子: 175
- 注册时间: 2006-04-29 20:10
- 来自: 北京·昌平
- 联系:
-
- 帖子: 14
- 注册时间: 2006-10-01 23:48
Re: 请大家帮我看看这个简单的c++程序为什么编译错误!
使用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);
前者的语法是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’ 在此作用域中尚未声明
-
- 帖子: 3
- 注册时间: 2006-11-13 21:53