By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,381 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,381 IT Pros & Developers. It's quick & easy.

Silly question (8 lines of code)

P: n/a
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 ();}
------------------------------

Thanks!
Dec 28 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Manuel wrote:
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 ();}
------------------------------

Thanks!


Because you are doing work outside of program flow. Areas outside of
functions/methods are for things such as declarations, not actual work.
Dec 28 '05 #2

P: n/a
Manuel wrote:
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 ();}
------------------------------

Thanks!

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
Dec 28 '05 #3

P: n/a
Manuel wrote:
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 ();}
------------------------------

Thanks!


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

Dec 28 '05 #4

P: n/a

"Manuel" <ma**********************@tin.it> wrote in message
news:43**********************@reader1.news.tin.it. ..
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.

} ------------------------------

Thanks!


-Mike
Dec 28 '05 #5

P: n/a
Peter Jansson wrote:
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
Dec 28 '05 #6

P: n/a
Mike Wahler wrote:
CRectangle rect;
rect.set_values ();

Executable statements must be inside a function.


I was thinking set_values() itself was already a function...
Dec 28 '05 #7

P: n/a
mlimber wrote:
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):
--------------
error: expected init-declarator before '.' token
error: expected `,' or `;' before '.' token
--------------

THANKS!!

Dec 28 '05 #8

P: n/a

"Manuel" <ma**********************@tin.it> wrote in message
news:43**********************@reader3.news.tin.it. ..
Mike Wahler wrote:
CRectangle rect;
rect.set_values ();

Executable statements must be inside a function.


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


Dec 28 '05 #9

P: n/a
Howard wrote:
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 !
:-)
Dec 28 '05 #10

P: n/a
On Wed, 28 Dec 2005 15:31:59 +0000, Howard wrote:
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

Dec 28 '05 #11

P: n/a
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.

Dec 29 '05 #12

P: n/a
Murali Krishna wrote:
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

Dec 29 '05 #13

P: n/a
Murali Krishna wrote:
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
Dec 29 '05 #14

P: n/a

Manuel wrote in message <43**********************@reader1.news.tin.it>.. .
Murali Krishna wrote:
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


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/
Dec 29 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.