using the vector template class
Rupert Wood
me@rupey.net
Wed Jan 2 06:21:00 GMT 2002
David Churches wrote:
> I am trying to use GCC to compile C++ code, but I am unable to use
> various classes such as vector and valarray. To illustrate this, I
> have been trying to compile the following piece of very simple code.
:
> #include <vector>
>
> int main ()
> {
>
> vector <int> ivec(5);
> return 0;
> }
libstdc++-v3's headers (correctly) only define vector in the 'std'
namespace and not the global namespace: i.e. you must qualify 'vector'
with 'std::'
#include <vector>
int main ()
{
std::vector<int> ivec(5);
return 0;
}
or import std::vector into the global namespace:
#include <vector>
using std::vector;
int main ()
{
vector<int> ivec(5);
return 0;
}
or import the whole std namespace:
#include <vector>
using namespace std;
int main ()
{
vector<int> ivec(5);
return 0;
}
I prefer the first or second solution for source files (depending on how
much 'vector' et al are used) but always the first solution for header
files - IMO, a header file should not pollute namespaces of a source
file that includes it. The third solution is perhaps the simplest but
often regarded as poor style: it is less precise that the other two.
Hope that helps,
Rupert.
More information about the Gcc-help
mailing list