Question about an extern int

E

exits funnel

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
 
P

Pete Becker

exits said:
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:
 
V

Victor Bazarov

exits funnel said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top