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

R

Roman Werpachowski

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.
 
T

Thomas Tutone

Roman said:
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
 
R

Roman Werpachowski

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!
 
P

puzzlecracker

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?
 
D

Dietmar Kuehl

Roman said:
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.
 
J

Jonathan Mcdougall

Roman said:
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
 
R

Roman Werpachowski

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.
 
D

Dave

Roman said:
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)
 
D

Dave

Dave said:
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)
 
N

Niklas Norrthon

Jonathan Mcdougall said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top