Re: likely a supid problem
On Oct 2, 10:16 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
w...@seed.net.tw wrote:
On 10?2?, ??2?57?, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Rolf Magnus wrote:
w...@seed.net.tw wrote:
Hi:
I made a simple code(t.cpp) like below
#include <iostream>
int main()
{
std::cout << "helo! world.\n";
return(0);
};
$gcc t.cpp (causes the following dumps. What's the problem?)
You used gcc instead of g++.
... and put a semicolon after a global function...
... and misspelled 'hello'. :-)
I used to put semicolons after an expression as possible.
Because it is easier to spot errors mechanically for me and
for compiler to report errors sooner. e.g.
struct A {
int f(int n) {
int i=0;
while(i<n) {
++i;
}; // this semicolon is often seen absent
That's fine. You have an empty statement after the body of the
'while' statement.
Which is confusing to the reader, and could cause problems if an
if/else is later introduced around the while statement.
And just a nit: you know that it's an empty statement, and I
know it, but the C++ standard doesn't; there's no such thing as
an empty statement in C++. According to the standard, this is
an expression statement. Which has repercusions, because an
expression statement isn't legal except in a function.
do { --i;
if(i==1) {
} else {
}; // this semicolon is often seen absent
Again, you have an empty statement after the body of the
'else' part.
if(i==1) {
}; // this semicolon is often seen absent
Here too, you have an empty statement after the body of the
'if'.
An empty statement which isn't part of the if statement. Which
can lead to interesting consequences if you add an else.
Consider:
if ( a )
if ( b ) {
c ;
} ;
else { // This else associates with the first
// if, not the second.
}
} while(i>=0);
for(i=0; i<n; ++i) {
}; // this semicolon is often seen absent
Another empty statement.
for(i=0; i<n; ++i) try{
}
catch(...) {
return -1;
}; // this semicolon is often seen absent
And another one.
};
This is a superfluous, yet harmless, semicolon. It's
*explicitly* allowed in the body of a class (or a struct).
}; // end struct A
Is this a valid C++ program?
Yes, that's a valid C++ program.
> Even f() is global? Thanks first.
No, if 'f' is global, the semicolon following its body is a
syntax error. Empty statements are OK inside a function and
inside a class definition. Empty statements are *not* OK in
the namespace scope.
IMHO, it would be better if the standard did define empty
statements, and allowed them anywhere a declaration is allowed.
As it is, this is an expression statement, and of course,
expression statements are only allowed in a function. (Defining
empty statements this way would also eliminate the special case
for a semicolon after a function definition in a class.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34