星期三, 2月 08, 2006

奇怪的gnu g++ : include stl header

好奇怪,雖然
#include <iostream>
也可以,但是使用
cout
會出現"undeclared name" Error。
所以要用
#include <iostream.h>
但是compile時卻又出現warnning,建議不要使用.h字尾....



還有...

stdlib.h 這一個header file,在STLport和 libc 中都有。但是STLport的stdlib.h 卻沒有東西(都是comment),所以include path (-I option) 若是將STLport include path 先寫,stdlib.h 會include 到STLport的版本,導致 stdlib.h 中的function 都不能使用。



原來是因為自己的c++程度不夠的關係。
這是namespace:iostream的宣告:
namespace std{
extern istream cin;
extern ostream cout;
....
}
所以在引用所有iostream內宣告的變數名稱時,都要加上name: std。
也就是說,source code要改成:
int main()
{
std::cout << "Hello";
}
或是在code中加入
using namespace std;

c++ 的namespace是為了防止name-pollution,也就是防止很多.C, .h 宣告出同樣的variable/class/method name。

有宣告namespace的變數,實際reference時都要在前面加上name ::

沒有留言:

網誌存檔