Re: likely a supid problem
On Oct 2, 10:05 pm, 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:
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.
Putting a semicolon after an expression turns it into a
statement. If you want or need a statement, the semicolon is
required. If you don't want or need a statement, it's
forbidden. There's no case where you have a choice about a
semicolon after an expression.
struct A {
int f(int n) {
int i=0;
while(i<n) {
++i;
}; // this semicolon is often seen absent
Always absent, I would say. A while statement is terminated by
the statement it controls, and in practice, all statements are
terminated by either a closing brace or a semicolon. A
semicolon here only introduces an additional statement for
nothing, and can cause problems, e.g.:
if ( x )
while ( i < n ) {
++ i ;
}
else ...
Adding a semicolon after the while would cause an error.
do { --i;
if(i==1) {
} else {
}; // this semicolon is often seen absent
See above. Same problem. It's absent because it can cause
problems.
if(i==1) {
}; // this semicolon is often seen absent
} while(i>=0);
for(i=0; i<n; ++i) {
}; // this semicolon is often seen absent
for(i=0; i<n; ++i) try{
}
catch(...) {
return -1;
}; // this semicolon is often seen absent
};
The above is really the only context where a semicolon is really
optional, and doesn't do anything.
}; // end struct A
Is this a valid C++ program? Even f() is global?
All of these exact examples are legal, but you do have to be
careful. A semi-colon where one is not needed, EXCEPT after the
definition of a member function in the class, creates an
expression statement. That's an additional statement, which
isn't legal in all contexts, and can change the meaning in
sometimes unexpected ways in other contexts.
--
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