Silly question (8 lines of code)

M

Manuel

Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------


And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;


int main () {rect.set_values ();}
 
W

W Marsh

Manuel said:
Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------


And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;


int main () {rect.set_values ();}

Because you are doing work outside of program flow. Areas outside of
functions/methods are for things such as declarations, not actual work.
 
P

Peter Jansson

Manuel said:
Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------


And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;


int main () {rect.set_values ();}


Hello,

In your first non-working example, what is going wrong (please provide
the complete error message) and what do you expect from the program? My
guess is that the compiler produces code that does nothing since you do
nothing in your main?

Best regards,
Peter
 
M

mlimber

Manuel said:
Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

int main () {}
------------------------------


And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;


int main () {rect.set_values ();}

A program is a *sequence* of instructions, which in C++ means that when
all initialization is complete, main() is invoked. All program behavior
must result from one of those two steps. In your first example, the
call to CRectangle::set_values() doesn't fit in that scheme since it is
neither part of initialization nor a consequence of calling main(). In
the second example, it is the latter. You could make the call part of
the pre-main initialization like this:

struct CRectangle
{
bool set_values() { return true; }
};

namespace // anonymous namespace for file-scope variables
{
CRectangle rect;
const bool init = rect.set_values();
}

int main() {}

Cheers! --M
 
M

Mike Wahler

Manuel said:
Why this not work?
-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;
rect.set_values ();

Executable statements must be inside a function.

This one is nt.
int main () {}
------------------------------


And this work?

-----------------------------
class CRectangle {
public:
void set_values ();
};

void CRectangle::set_values () {}

CRectangle rect;


int main () {

rect.set_values ();

This executable statement is inside a function, so it's OK.

}
-Mike
 
M

Manuel

Peter said:
In your first non-working example, what is going wrong (please provide
the complete error message) and what do you expect from the program? My
guess is that the compiler produces code that does nothing since you do
nothing in your main?

No...the error is strange:

error: expected init-declarator before '.' token
error: expected `,' or `;' before '.' token
 
M

Manuel

mlimber said:
A program is a *sequence* of instructions, which in C++ means that when
all initialization is complete, main() is invoked. All program behavior
must result from one of those two steps. In your first example, the
call to CRectangle::set_values() doesn't fit in that scheme since it is
neither part of initialization nor a consequence of calling main(). In
the second example, it is the latter. You could make the call part of
the pre-main initialization like this:

OK...I think to have understand: the keyword is *sequence*: the function
was outside the main and was "alone", not called in calls tree started
from main(). It's correct?

However the error was very strange (make me crazy):
 
H

Howard

Manuel said:
I was thinking set_values() itself was already a function...

It is a _call_ to a function. Perhaps what he should have said was
"Executable statements must be inside a function _definition_."
Declarations of variables (including initialization of those variables) may
reside at global scope, but not executable statements which are _not_
initialization sequences.

This is legal:

int MyGlobal = 5;

This is also legal:

int MyGlobal ;
void f()
{
MyGlobal = 5;
}

But this is not:

int MyGlobal ;
MyGlobal = 5;

The error message was caused by an invalid sequence of tokens. The parser
saw the token "rect" in the line "rect.set_calues ();", and recognized that
as an identifier (i.e., a name for something). So far, that's not
_neccessarily_ an error. But the "." following it is, because there is no
legal statement that the parser could identify which begins with "rect.",
(at least not when outside of any function definition).

That's the way with error messages...sometimes you have to look at the code
_before_ what it flags as an error, or at the whole statement containing the
error, not just at the one symbol or identifier that it flags as an error.
In this case, it's the whole statement that's illegal (when outside of any
function definition).

-Howard
 
M

Manuel

Howard said:
The error message was caused by an invalid sequence of tokens. The parser
saw the token "rect" in the line "rect.set_calues ();", and recognized that
as an identifier (i.e., a name for something). So far, that's not
_neccessarily_ an error. But the "." following it is, because there is no
legal statement that the parser could identify which begins with "rect.",
(at least not when outside of any function definition).

That's the way with error messages...sometimes you have to look at the code
_before_ what it flags as an error, or at the whole statement containing the
error, not just at the one symbol or identifier that it flags as an error.
In this case, it's the whole statement that's illegal (when outside of any
function definition).

Thanks.
It's all clear now !
:)
 
J

Jay Nabonne

It is a _call_ to a function. Perhaps what he should have said was
"Executable statements must be inside a function _definition_."
Declarations of variables (including initialization of those variables) may
reside at global scope, but not executable statements which are _not_
initialization sequences.

This is legal:

int MyGlobal = 5;

This is also legal:

int MyGlobal ;
void f()
{
MyGlobal = 5;
}

But this is not:

int MyGlobal ;
MyGlobal = 5;

However, this is also legal:

int f()
{
return 5;
}

int MyGlobal = f();

even outside main.

So function calls for purposes of initialization are valid.

- Jay
 
M

Murali Krishna

I am afraid. How are your programs working with out returning any
values from main()

int main()
{
return 0; // finally my compiler asks me to return an integer
// if I specified return type as int
// and if return type is void you may not return any
thing.
}

Plz let know what compiler you are using.
 
J

Jonathan Mcdougall

Murali said:
I am afraid. How are your programs working with out returning any
values from main()

int main()
{
return 0; // finally my compiler asks me to return an integer
// if I specified return type as int
// and if return type is void you may not return any
thing.
}

Two things:

1) not returning anything from main() is like returning 0

int main()
{
}

and

int main()
{
return 0;
}

are both valid and they do the exact same thing.

2) void main() is illegal. The only return type main() can have is int.


Jonathan
 
M

Manuel

Murali said:
I am afraid. How are your programs working with out returning any
values from main()

int main()
{
return 0; // finally my compiler asks me to return an integer
// if I specified return type as int
// and if return type is void you may not return any
thing.
}

Plz let know what compiler you are using.



Dev c++

thx,

Manuel
 
B

BobR

Manuel wrote in message said:
Dev c++
thx,
Manuel

FYI: That is an IDE. The default compiler for DevC++ is GCC, MinGW port.
You can set-up DevC++ to use other compilers (..but, it's not fun!! <G>).

--
Bob R
POVrookie
--
Dev-C++ IDE: http://www.bloodshed.net/
MinGW (GNU compiler): http://www.mingw.org/
MinGWStudio http://www.parinyasoft.com/
wxWidgets URL: http://www.wxwidgets.org
V IDE & V GUI: http://www.objectcentral.com/
Quincy IDE 2005 URL: http://pipou.net/down/Quincy2005Project.zip
POVray: http://www.povray.org/
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top