大家好。小弟最近在嘗試使用 STL ( Standard Template Library )。
比較頭痛的事情是小弟是在 UNIX 環境之下工作, 據說 gcc 2.7.0 開始就有支援
STL, 而我的工作環境就是 gcc 2.7.0, 但是僅僅編譯一個小程式 ( 是
STL Tutorial and Reference , Musser and Saini
這本書上的範例 )都沒有辦法過關, 請各位指教。程式本身如下:
// Using the STL generic reverse algorithm with a vector.
#include <iostream.h>
#include <algo.h>
#include <vector.h
#include <assert.h>
vector<char> vec(char* s)
// Return vector<char> containing the characters of s
// (not including the terminating null).
{
vector<char> x;
while (*s != '\0')
x.push_back(*s++);
return x;
}
int main()
{
cout << "Using reverse algorithm with a vector" << endl;
vector<char> vector1 = vec("mark twain");
reverse(vector1.begin(), vector1.end());
assert(vector1 == vec("niawt kram"));
}
在 compile 時, 若 g++ ex01-03.cpp 時會出現
/usr/local/lib/g++-include/algobase.h: In function `void iter_swap(char *, char
*)':
/usr/local/lib/g++-include/algobase.h:31: call of overloaded `value_type' is amb
iguous
/usr/local/lib/g++-include/vector.h:180: candidates are: value_type(char *const
&)
/usr/local/lib/g++-include/iterator.h:90: value_type(const char
*)
這些錯誤訊息, 而若我改用 g++ -I../stl ex01-03.cpp 的話則會有
../stl/defalloc.h: In function `void * operator new(long unsigned int, void *)':
In file included from ../stl/vector.h:25,
from ex01-03.cpp:4:
../stl/defalloc.h:27: redefinition of `void * operator new(long unsigned int, vo
id *)'
/usr/local/lib/g++-include/std/new.h:29: `void * operator new(long unsigned int,
void *)' previously defined here
../stl/vector.h: At top level:
In file included from ex01-03.cpp:4:
../stl/vector.h:171: parse error before `<'
../stl/vector.h:175: confused by earlier errors, bailing out
這些錯誤訊息。其中 ../stl 中包含了我從網路上抓下來的 HP Implementation ( 1995
Oct. 31 Release ); 而在 vector.h 中第 171 line 是
template <class T> vector<T>::vector_allocator vector<T>::static_allocator;
實在是不知道為什麼會 parse error. 請問各位先進在這種情況下是要如何能夠
安心地使用 STL 呢?