| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"exits funnel" |
| Date: |
07 Dec 2003 01:58:27 PM |
| Object: |
Question about an extern int |
Hello,
I have the following three files
//BEGIN test.h
class foo
{
public:
void print( );
private:
static long int sint;
static char pool[]; // (1)
};
//END test.h
//BEGIN test.cpp
#include <iostream>
#include "test.h"
extern long int number;
long int foo::sint = number; // (3)
char foo::pool[number]; // (2)
void foo::print( )
{
cout << "the number is " << number << endl;
cout << "sint = " << sint << endl;
}
//END test.cpp
//BEGIN testmain.cpp
#include "test.h"
long int number = 14;
int main( )
{
foo t;
t.print( );
}
//END testmain.cpp
If I comment out the lines marked (1) and (2) everything compiles and
runs as expected but with them umcommented my compiler complains thusly:
test.cpp:7: variable-size type declared outside of any function
test.cpp:7: variable-size type declared outside of any function
test.cpp:7: confused by earlier errors, bailing out
It's not clear to me why the extern int is a problem in line (2) but not
in line (3). If anyone could explain the compiler's rationale I would
be most grateful. Thanks.
-exits
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Question about an extern int |
07 Dec 2003 02:24:45 PM |
|
|
"exits funnel" <exitsfunnel@NOSPAMyahoo.com> wrote...
I have the following three files
//BEGIN test.h
class foo
{
public:
void print( );
private:
static long int sint;
static char pool[]; // (1)
};
//END test.h
//BEGIN test.cpp
#include <iostream>
#include "test.h"
extern long int number;
long int foo::sint = number; // (3)
char foo::pool[number]; // (2)
void foo::print( )
{
cout << "the number is " << number << endl;
cout << "sint = " << sint << endl;
}
//END test.cpp
//BEGIN testmain.cpp
#include "test.h"
long int number = 14;
int main( )
{
foo t;
t.print( );
}
//END testmain.cpp
If I comment out the lines marked (1) and (2) everything compiles and
runs as expected but with them umcommented my compiler complains thusly:
test.cpp:7: variable-size type declared outside of any function
test.cpp:7: variable-size type declared outside of any function
test.cpp:7: confused by earlier errors, bailing out
It's not clear to me why the extern int is a problem in line (2) but not
in line (3). If anyone could explain the compiler's rationale I would
be most grateful. Thanks.
You may initialise an object with any expression (non-const is just as
fine as const), but you _must_ supply only a _compile-time_ constant
expression as a size of an array.
In your case 'number' is not a compile-time constant because it's not
known to the compiler when it compiles "test.cpp".
Victor
.
|
|
|
|
| User: "Biyou Tang" |
|
| Title: Re: Question about an extern int |
07 Dec 2003 07:23:38 PM |
|
|
exits funnel <exitsfunnel@NOSPAMyahoo.com> wrote in message news:<3FD390C8.5030408@NOSPAMyahoo.com>...
the number must be const type
.
|
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Question about an extern int |
07 Dec 2003 02:22:42 PM |
|
|
exits funnel wrote:
test.cpp:7: variable-size type declared outside of any function
test.cpp:7: variable-size type declared outside of any function
The reason the compiler doesn't complain about line 8 is:
test.cpp:7: confused by earlier errors, bailing out
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Question about an extern int |
07 Dec 2003 02:46:09 PM |
|
|
Pete Becker wrote:
exits funnel wrote:
test.cpp:7: variable-size type declared outside of any function
test.cpp:7: variable-size type declared outside of any function
The reason the compiler doesn't complain about line 8 is:
test.cpp:7: confused by earlier errors, bailing out
Sorry, I misread the code.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
.
|
|
|
|
|

|
Related Articles |
|
|