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

Question about an extern int

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

Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
Jul 22 '05 #2

P: n/a
"exits funnel" <ex*********@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
Jul 22 '05 #3

P: n/a
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)
Jul 22 '05 #4

P: n/a
exits funnel <ex*********@NOSPAMyahoo.com> wrote in message news:<3F**************@NOSPAMyahoo.com>...
the number must be const type
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.