likely a supid problem


W

wij

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

/tmp/ccsRiwha.o(.text+0xd): In function `std::__verify_grouping(char
const*, unsigned int, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)':
: undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::size() const'
/tmp/ccsRiwha.o(.text+0x60): In function `std::__verify_grouping(char
const*, unsigned int, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)':
: undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::eek:perator[](unsigned
int) const'
/tmp/ccsRiwha.o(.text+0x9d): In function `std::__verify_grouping(char
const*, unsigned int, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)':
: undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::eek:perator[](unsigned
int) const'
"t" 30L, 2072C ¤w¼g¤J
 
Ad

Advertisements

R

Rolf Magnus

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++.
 
W

wij

... and put a semicolon after a global function...
... and misspelled 'hello'. :)

V

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

do { --i;
if(i==1) {
} else {
}; // this semicolon is often seen absent

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
};

}; // end struct A

Is this a valid C++ program? Even f() is global? Thanks first.
 
J

James Kanze

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

Advertisements

J

James Kanze

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.
Again, you have an empty statement after the body of the
'else' part.
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.
}

Another empty statement.
And another one.

This is a superfluous, yet harmless, semicolon. It's
*explicitly* allowed in the body of a class (or a struct).
Yes, that's a valid C++ program.
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.)
 

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

Top