473,785 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 22 '05 #1
4 2030
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
"exits funnel" <ex*********@NO SPAMyahoo.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
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
exits funnel <ex*********@NO SPAMyahoo.com> wrote in message news:<3F******* *******@NOSPAMy ahoo.com>...
the number must be const type
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1921
by: mark | last post by:
compiler g++ on linux file.h has the following, int x=5; and inside the main program file I can access "x" without declaring it "extern x". Program compiles and runs fine.... also program compiles and runs fine with x declared as extern. So is it necessary to declare x as extern if one get away without declaring it extern ? What is the standard and good practice ?
110
4529
by: Vijay Kumar R Zanvar | last post by:
Hi, Which section of C99 says that return value of malloc(3) should not be casted? Thanks. -- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
5
3307
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors. The four files are main.c, main.h, readLP.h, and readLP.c. In readLP.h
7
5535
by: Kevin | last post by:
Hi al I have an interesting question.... I am working witha Win API this is the Function Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Lon Which I got from the API viewer that comes with VB 6. I have tried to convert it to the following ...
2
12360
by: Al Bahr | last post by:
H I am try to convert C# to vb.net I don’t know what would be an equivalent statements in VB.net any help will be appreciated. bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat.. ' <summary ' Palette entry structur ' </summary bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat..
0
1148
by: Al Bahr | last post by:
H I am try to convert C# to vb.net I don’t know what would be an equivalent statements in VB.net any help will be appreciated bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat.. ' <summary ' Palette entry structur ' </summary bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat..
2
2035
by: JoeC | last post by:
I am trying to create a message box that allows you to enter a name then display that name on a window. The program uses win32 but that is not the issue. I want have a global buf variable. So that it can be filled in the window then displayed in the main window. Here is what I have char * buf = NULL; //In my min program. extern char * buf; // This is the section where I want buf displayed.
5
1937
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern" qualifier - it appears to work: x is shared between w.c and ww.c. Or if I simple don't use "extern", it works. Why? What is the correct (or simply preferred) usage? ---John PSI'm using gcc (GCC) 4.0.2.
17
1737
by: Francine.Neary | last post by:
I have a program that uses a large lookup table, provided as a large array in the source: static int bigtbl={123, 456, /* etc. etc. */ 9999 }; Now this table is pretty big, and having it clutter up my main source file is quite ugly. So I'd like to put it in a separate file and #include that.
6
1491
by: vaclavpich | last post by:
Hi, I have a question on constant variables. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // #include <cstdlib> #include <iostream> using namespace std; const static char* STATIC_CONST_NAME = "Hello";
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.