473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Q: Using an extern const size_t?

Hi,

I have the following scenario:

--- file1.cpp ---

size_t const array_size = 10;

--- end file1.cpp ---

--- file2.cpp ---

class test
{
int m_nums [array_size];
};

--- end file2.cpp ---

My compiler, VC++7.1, complains about not having a constant expression
for the array. Why can the array size not be 'calculated' at compile-time?
Or is it just a bug in VC++?

Thanks!
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #1
5 1506
"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:c1*************@news.t-online.com...

I forgot a line:

--- file1.cpp ---

size_t const array_size = 10;

--- end file1.cpp ---

--- file2.cpp ---

extern size_t const array_size;

class test
{
int m_nums [array_size];
};

--- end file2.cpp ---

Sorry about that
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #2

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:c1*************@news.t-online.com...
Hi,

I have the following scenario:

--- file1.cpp ---

size_t const array_size = 10;

--- end file1.cpp ---

--- file2.cpp ---

I guessing that you missed out

extern const size_t array_size;

here.
class test
{
int m_nums [array_size];
};

--- end file2.cpp ---

My compiler, VC++7.1, complains about not having a constant expression
for the array. Why can the array size not be 'calculated' at compile-time?
Or is it just a bug in VC++?


Put

size_t const array_size = 10;

in a header file and include that header file in file1.cpp and file2.cpp.
This is OK because in C++ constants have internal linkage by default so you
won't get an error about array_size being declared twice.

john
Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:c1*************@news.t-online.com...
Hi,

I have the following scenario:

--- file1.cpp ---

size_t const array_size = 10;

--- end file1.cpp ---

--- file2.cpp ---

I guessing that you missed out

extern const size_t array_size;

here.


Yes, see my reply to myself ;)
class test
{
int m_nums [array_size];
};

--- end file2.cpp ---

My compiler, VC++7.1, complains about not having a constant expression for the array. Why can the array size not be 'calculated' at compile-time? Or is it just a bug in VC++?


Put

size_t const array_size = 10;

in a header file and include that header file in file1.cpp and file2.cpp.
This is OK because in C++ constants have internal linkage by default so

you won't get an error about array_size being declared twice.


Right, but why does the above example not work? Theoretically, it
should, or what am I missing?
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #4
Jakob Bieling wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:c1*************@news.t-online.com...
Hi,

I have the following scenario:

--- file1.cpp ---

size_t const array_size = 10;

--- end file1.cpp ---

--- file2.cpp ---


I guessing that you missed out

extern const size_t array_size;

here.

Yes, see my reply to myself ;)

class test
{
int m_nums [array_size];
};

--- end file2.cpp ---

My compiler, VC++7.1, complains about not having a constant
expression
for the array. Why can the array size not be 'calculated' at
compile-time?
Or is it just a bug in VC++?


Put

size_t const array_size = 10;

in a header file and include that header file in file1.cpp and file2.cpp.
This is OK because in C++ constants have internal linkage by default so


you
won't get an error about array_size being declared twice.

Right, but why does the above example not work? Theoretically, it
should, or what am I missing?


You are missing the fact that the value of `array_size' will not be
known until link time (as it is defined in a different translation unit).

HTH,
--ag

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Jul 22 '05 #5
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:c1*************@ID-219787.news.uni-berlin.de...
--- file1.cpp ---

size_t const array_size = 10;

--- end file1.cpp ---

--- file2.cpp --- extern const size_t array_size; class test
{
int m_nums [array_size];
};

--- end file2.cpp ---
Right, but why does the above example not work? Theoretically, it
should, or what am I missing?


You are missing the fact that the value of `array_size' will not be
known until link time (as it is defined in a different translation unit).


Oh .. makes sense now, yes!

Thanks!
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #6

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

Similar topics

4
by: Umesh | last post by:
Hi all I am trying to change the printer(like paper source ,etc) setting in C#. But i am unable to do it. I am not sure how to do it. should i have to use WIN API like Openprinter , getprinter ,...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
4
by: Oliver | last post by:
Hello ! I am trying to get the unrar.dll working in C#... it seems that I correctly imported the functions as the first 2 function work without problem (RAROpenArchive & RARGetDLLVersion)......
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
2
by: mmmobasher | last post by:
Dear sirs after some googling i found some code to using unrar.dll, but i get runtime error System.NullReferenceException in line iStatus=RARReadHeader(lHandle, ref uHeader); hear is the code ...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
14
by: Mr John FO Evans | last post by:
I cam across an interesting limitation to the use of strtok. I have two strings on which I want strtok to operate. However since strtok has only one memory of the residual string I must complete...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.