"using namespace" within a class declaration?

Discussion in 'C++' started by Jacek Dziedzic, Apr 28, 2004.

  1. Is it valid to use a "using namespace foo" (as opposed to
    using foo::bar which I'm sure is legal) within a class
    declaration? My compiler rejects it, but I've been told it's
    valid.

    Can anyone please confirm or deny?

    TIA,
    - J.
    Jacek Dziedzic, Apr 28, 2004
    #1
    1. Advertising

  2. Jacek Dziedzic wrote:

    > Is it valid to use a "using namespace foo"
    > (as opposed to using foo::bar which I'm sure is legal)
    > within a class declaration?
    > My compiler rejects it, but I've been told it's valid.
    >
    > Can anyone please confirm or deny?



    > cat test.cc

    #include <iostream>

    class X {
    private:
    // representation
    int I;
    public:
    using namespace std;
    using std::endl;
    X(int i = 0): I(i) {
    cout << "X(" << i << ")" << endl;
    }
    };

    int main(int argc, char* argv[]) {
    X x(13);
    std::cout << "The end!" << std::endl;
    return 0;
    }

    > g++ -Wall -ansi -pedantic -o test test.cc

    test.cc:8: parse error before `namespace'
    test.cc:9: using-declaration for non-member at class scope
    test.cc: In constructor `X::X(int)':
    test.cc:11: `cout' undeclared (first use this function)
    test.cc:11: (Each undeclared identifier is reported \
    only once for each function it appears in.)
    test.cc:11: `endl' undeclared (first use this function)
    test.cc: In function `int main(int, char**)':
    test.cc:16: warning: unused variable `X x'
    E. Robert Tisdale, Apr 28, 2004
    #2
    1. Advertising

  3. Jacek Dziedzic

    John Carson Guest

    "Jacek Dziedzic" <> wrote in message
    news:c6p955$hje$
    > Is it valid to use a "using namespace foo" (as opposed to
    > using foo::bar which I'm sure is legal) within a class
    > declaration? My compiler rejects it, but I've been told it's
    > valid.
    >
    > Can anyone please confirm or deny?
    >
    > TIA,
    > - J.


    Both Comeau online and VC++ 7.1 reject both and say that using declarations
    must involve base class names. For example, the following won't compile:

    #include <iostream>
    class A
    {
    public:
    using std::cout;
    A()
    {
    cout << "A constructed\n";
    }
    };

    nor will

    #include <iostream>
    class A
    {
    public:
    using namespace std;
    A()
    {
    cout << "A constructed\n";
    }
    };

    On the other hand, if you move the using directive/declaration inside a
    function, it is a different story. The following is fine:

    class A
    {
    public:
    A()
    {
    using namespace std;
    using std::cout;
    cout << "A constructed\n";
    }
    };


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Apr 29, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Walt Karas
    Replies:
    5
    Views:
    323
    Walt Karas
    Sep 5, 2004
  2. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,353
    Howard
    Nov 29, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    500
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,221
    Richard Herring
    May 17, 2005
  5. Andrew Ward
    Replies:
    5
    Views:
    292
    Andrew Ward
    Jul 25, 2005
Loading...

Share This Page