Is 'close' a reserved word in C++ ?

Discussion in 'C++' started by Roman Werpachowski, Dec 29, 2005.

  1. The following code does not compile:

    #include <iostream>

    class close {
    public:
    void message();
    };

    inline void close::message()
    {
    std::cout << "Close class\n";
    }

    int main()
    {
    close c;

    c.message();
    }

    The message given by g++ is:

    other.cc: In function `int main()':
    other.cc:15: error: `close' undeclared (first use this function)
    other.cc:15: error: (Each undeclared identifier is reported only once for each
    function it appears in.)
    other.cc:15: error: syntax error before `;' token
    other.cc:17: error: `c' undeclared (first use this function)

    I don't understand it. I've looked for a list of reserved words in C++ and
    'close' is not there.

    --
    Roman Werpachowski
    /--------==============--------\
    | http://www.cft.edu.pl/~roman |
    \--------==============--------/
     
    Roman Werpachowski, Dec 29, 2005
    #1
    1. Advertising

  2. Roman Werpachowski wrote:
    > The following code does not compile:
    >
    > #include <iostream>
    >
    > class close {
    > public:
    > void message();
    > };
    >
    > inline void close::message()
    > {
    > std::cout << "Close class\n";
    > }
    >
    > int main()
    > {
    > close c;
    >
    > c.message();
    > }
    >
    > The message given by g++ is:
    >
    > other.cc: In function `int main()':
    > other.cc:15: error: `close' undeclared (first use this function)
    > other.cc:15: error: (Each undeclared identifier is reported only once for each
    > function it appears in.)
    > other.cc:15: error: syntax error before `;' token
    > other.cc:17: error: `c' undeclared (first use this function)
    >
    > I don't understand it. I've looked for a list of reserved words in C++ and
    > 'close' is not there.


    close is not a reserved word in C++. Comeau compiles your program
    fine. What version of gcc are you using?

    Best regards,

    Tom
     
    Thomas Tutone, Dec 29, 2005
    #2
    1. Advertising

  3. On the 28 Dec 2005 17:04:18 -0800, Thomas Tutone wrote:

    >> I don't understand it. I've looked for a list of reserved words in C++ and
    >> 'close' is not there.

    >
    > close is not a reserved word in C++. Comeau compiles your program
    > fine. What version of gcc are you using?


    3.3.6 compiled for x86_64


    --
    Roman Werpachowski
    /--------==============--------\
    | http://www.cft.edu.pl/~roman |
    \--------==============--------/
     
    Roman Werpachowski, Dec 29, 2005
    #3
  4. On the Thu, 29 Dec 2005 01:08:16 +0000 (UTC), Roman Werpachowski wrote:
    > On the 28 Dec 2005 17:04:18 -0800, Thomas Tutone wrote:
    >
    >>> I don't understand it. I've looked for a list of reserved words in C++ and
    >>> 'close' is not there.

    >>
    >> close is not a reserved word in C++. Comeau compiles your program
    >> fine. What version of gcc are you using?

    >
    > 3.3.6 compiled for x86_64


    The darn thing compiles when I remove

    #include <iostream>

    line.

    --
    Roman Werpachowski
    /--------==============--------\
    | http://www.cft.edu.pl/~roman |
    \--------==============--------/
     
    Roman Werpachowski, Dec 29, 2005
    #4
  5. On the Thu, 29 Dec 2005 01:11:17 +0000 (UTC), Roman Werpachowski wrote:
    > On the Thu, 29 Dec 2005 01:08:16 +0000 (UTC), Roman Werpachowski wrote:
    >> On the 28 Dec 2005 17:04:18 -0800, Thomas Tutone wrote:
    >>
    >>>> I don't understand it. I've looked for a list of reserved words in C++ and
    >>>> 'close' is not there.
    >>>
    >>> close is not a reserved word in C++. Comeau compiles your program
    >>> fine. What version of gcc are you using?

    >>
    >> 3.3.6 compiled for x86_64

    >
    > The darn thing compiles when I remove
    >
    > #include <iostream>
    >
    > line.


    It seems there are close() methods defined in std:: namespace. But I did
    *not* write:

    using namespace std;

    in the original source!


    --
    Roman Werpachowski
    /--------==============--------\
    | http://www.cft.edu.pl/~roman |
    \--------==============--------/
     
    Roman Werpachowski, Dec 29, 2005
    #5

  6. > It seems there are close() methods defined in std:: namespace. But I did
    > *not* write:
    >
    > using namespace std;
    >


    it doesn't make sense. There is a different reason -- likely a COMPILE
    RELATED.

    anyone knows the exact rules for look up?
     
    puzzlecracker, Dec 29, 2005
    #6
  7. Roman Werpachowski wrote:
    > I don't understand it. I've looked for a list of reserved words in C++ and
    > 'close' is not there.


    The problem is almost certainly the declaration of POSIX' 'close()'
    function. This is declared via implicit inclusion of <unistd.h> from
    <iostream>. Although I would guess that most if not all implementations
    on POSIX systems include this header, I would consider it to be a bug
    in the implementation. However, conflicting with names of system
    functions in global scope is probably best avoided.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Dec 29, 2005
    #7
  8. Roman Werpachowski wrote:
    > The following code does not compile:
    >
    > #include <iostream>
    >
    > class close {
    > public:
    > void message();
    > };
    >
    > inline void close::message()
    > {
    > std::cout << "Close class\n";
    > }
    >
    > int main()
    > {
    > close c;
    >
    > c.message();
    > }
    >
    > The message given by g++ is:
    >
    > other.cc: In function `int main()':
    > other.cc:15: error: `close' undeclared (first use this function)
    > other.cc:15: error: (Each undeclared identifier is reported only once for each
    > function it appears in.)
    > other.cc:15: error: syntax error before `;' token
    > other.cc:17: error: `c' undeclared (first use this function)
    >
    > I don't understand it. I've looked for a list of reserved words in C++ and
    > 'close' is not there.


    You mean if you actually copy/paste what's written in your message you
    get these errors? This seems highly unlikely to me.


    Jonathan
     
    Jonathan Mcdougall, Dec 29, 2005
    #8
  9. On the 28 Dec 2005 18:12:30 -0800, Jonathan Mcdougall wrote:

    > You mean if you actually copy/paste what's written in your message you
    > get these errors?


    Yup.

    > This seems highly unlikely to me.


    So now there is two of us.


    --
    Roman Werpachowski
    /--------==============--------\
    | http://www.cft.edu.pl/~roman |
    \--------==============--------/
     
    Roman Werpachowski, Dec 29, 2005
    #9
  10. Roman Werpachowski

    Dave Guest

    Roman Werpachowski wrote:

    > I don't understand it. I've looked for a list of reserved words in C++ and
    > 'close' is not there.
    >


    I think you might be a bit unwise to choose a word that is not
    reservered, but one might reasonably think might be in a later revision
    of the dynamic C++ language. close_file is not descriptive, but less
    likely to become a reservered word in the future.


    --
    Dave K

    http://www.southminster-branch-line.org.uk/

    Please note my email address changes periodically to avoid spam.
    It is always of the form: month-year@domain. Hitting reply will work
    for a couple of months only. Later set it manually. The month is
    always written in 3 letters (e.g. Jan, not January etc)
     
    Dave, Dec 29, 2005
    #10
  11. Roman Werpachowski

    Dave Guest

    Dave wrote:
    > Roman Werpachowski wrote:
    >
    >> I don't understand it. I've looked for a list of reserved words in C++
    >> and
    >> 'close' is not there.
    >>

    >
    > I think you might be a bit unwise to choose a word that is not
    > reservered, but one might reasonably think might be in a later revision
    > of the dynamic C++ language. close_file is not descriptive,


    Sorry, that was supposed to be "close file is not only more descriptive ..."


    --
    Dave K

    http://www.southminster-branch-line.org.uk/

    Please note my email address changes periodically to avoid spam.
    It is always of the form: month-year@domain. Hitting reply will work
    for a couple of months only. Later set it manually. The month is
    always written in 3 letters (e.g. Jan, not January etc)
     
    Dave, Dec 29, 2005
    #11
  12. "Jonathan Mcdougall" <> writes:

    > Roman Werpachowski wrote:
    > > The following code does not compile:
    > >
    > > #include <iostream>
    > >
    > > class close {
    > > public:
    > > void message();
    > > };
    > >
    > > inline void close::message()
    > > {
    > > std::cout << "Close class\n";
    > > }
    > >
    > > int main()
    > > {
    > > close c;
    > >
    > > c.message();
    > > }
    > >
    > > The message given by g++ is:
    > >
    > > other.cc: In function `int main()':
    > > other.cc:15: error: `close' undeclared (first use this function)
    > > other.cc:15: error: (Each undeclared identifier is reported only once for each
    > > function it appears in.)
    > > other.cc:15: error: syntax error before `;' token
    > > other.cc:17: error: `c' undeclared (first use this function)
    > >
    > > I don't understand it. I've looked for a list of reserved words in C++ and
    > > 'close' is not there.

    >
    > You mean if you actually copy/paste what's written in your message you
    > get these errors? This seems highly unlikely to me.


    I copied / pasted the exact code written in the message and compiled it
    with gcc 3.4.1 and gcc 4.1.0 on FreeBSD, and I got similar but not
    identical error messages, so what seems highly unlikely to you seems
    highly likely to me.

    The reason is a bug in gcc's <iostream>, which includes a header, which
    includes a header, which includes a header ..., which includes <unistd.h>,
    which declares the function close() in the global namespace.

    <OT>
    Invokation of gcc with -E switch, and then grep are handy tools when
    dealing with such errors.
    </OT>

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 29, 2005
    #12
    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. metaperl
    Replies:
    32
    Views:
    917
    NickC
    Sep 15, 2006
  2. Delaney, Timothy (Tim)
    Replies:
    10
    Views:
    671
    Jason
    Sep 14, 2006
  3. Steve Holden
    Replies:
    0
    Views:
    396
    Steve Holden
    Sep 13, 2006
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    864
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Replies:
    3
    Views:
    168
    osmium
    Nov 6, 2013
Loading...

Share This Page