Question about an extern int



 DEVELOP > c-Plus-Plus > Question about an extern int

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

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



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER