public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: using the vector template class
       [not found] <616BE6A276E3714788D2AC35C40CD18D29E5F1@whale.softwire.co.uk>
@ 2002-01-02  6:21 ` Rupert Wood
  0 siblings, 0 replies; 2+ messages in thread
From: Rupert Wood @ 2002-01-02  6:21 UTC (permalink / raw)
  To: 'David Churches'; +Cc: gcc-help

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* using the vector template class
@ 2002-01-02  5:57 David Churches
  0 siblings, 0 replies; 2+ messages in thread
From: David Churches @ 2002-01-02  5:57 UTC (permalink / raw)
  To: gcc-help


Hi,

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. I am using 
RedHat Linux 7.0 running on a Dell Latitude laptop.

Here is the code:


#include <vector>

int main ()
{

   vector <int> ivec(5);
   return 0;
}


I try to compile it using the following command:

g++ -o trivial trivial.cc

The error message I get is

trivial.cc: In function `int main()':
trivial.cc:6: `vector' undeclared (first use this function)
trivial.cc:6: (Each undeclared identifier is reported only once for each 
   function it appears in.)
trivial.cc:6: parse error before `>' token


I have recently re-installed GCC, and it looks like I am not linking up 
to the header files. Do I have to set any flags so that GCC knows where 
the header files are ? I have tried using -I/usr/local/include/g++-v3 on 
the command line, but it makes no difference.


Thanks in advance for any help,

David Churches.



-------------------------------------------------------------
David Churches
Department of Physics and Astronomy
Cardiff University, PO Box 913, Cardiff, CF2 3YB, U.K.
Phone: + 44-29-20874785, 20875121 (direct line) Fax: + 44-29-20874056
d.churches@astro.cf.ac.uk

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-01-02 14:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <616BE6A276E3714788D2AC35C40CD18D29E5F1@whale.softwire.co.uk>
2002-01-02  6:21 ` using the vector template class Rupert Wood
2002-01-02  5:57 David Churches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).