• Advertisement
Sign in to follow this  

g++ cygwin "cannot appear in constant-expression" error

This topic is 4289 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a windows machine using cygwin with g++ version 3.4.4 Dynamically allocating a 3d array I'm getting the error next2.cpp:83: error: `numthick' cannot appear in a constant-expression following is relevenat code: this is where it gets defined, also notice numlamda variable: int *thickness, *wavelength, numlamda, numthick; this is the definition for the 3d array: std::complex<double> *beta; this is line 83 where I get the error, I'm attempting to ccreate the array now: beta = new std::complex<double>[numlamda][numthick][numlayers-1]; here is where numthick (also numlamda) is being populated: int lowbound, thickstep, upbound; std::cin >> lowbound >> thickstep >> upbound; numlamda = ceil((upbound-lowbound)/thickstep); numthick = ceil((upbound-lowbound)/thickstep); as far a I can tell numlamda and numthick are being treated exactly the same through the code, but one is erroring out while the other is not. i've looked at how I'm using it and i don't see that I'm violating any convention with numthick. yes its a global variable (i know, bad, will change later, kind of need results quick) any help appreciated thanks james

Share this post


Link to post
Share on other sites
Advertisement
C++ requires array dimensions to be known at compile-time. C99 on the other hand allows array dimensions to be unknown until runtime. It would appear that numlamda is not giving you an error because gcc allows some elements of C99 in C++ code as extensions to the C++ standard. Your best bet is to rewrite your code to use a std::vector (or nested std::vectors) or a Boost.Multi_Array.

Ooops. Ignore me (except for the advice about std::vector or Boost.Multi_Array). Somehow I'd misread your code and completely missed the new keyword. omgomghilol is entirely correct although std::vector/Boost.Multi_Array are far superior to manual memory management.

Σnigma

[Edited by - Enigma on May 29, 2006 10:44:58 AM]

Share this post


Link to post
Share on other sites
Actually the problem here is that you can't use new to allocate a 2D or 3D array. You can either do:

beta = new std::complex<double>[numlambda * numthick * (numlayers - 1)];

And then come up with an access scheme, or do something like (pseudo-code):


x **y;
y = new x*[whatever];
for each element of y
y = new x[whatever];



Except in 3D. I highly recommend the first thing; Google will show you how to acess the elements of it (in 3D it's a bit tricky).

Share this post


Link to post
Share on other sites
RTFM. And then use a proper tool (such as the ones previously mentioned) rather than reinventing the wheel.

Share this post


Link to post
Share on other sites
"why re-invent the wheel?"
cause I was taught to learn how things worked, and that the more I knew the underpinnings of things the more I could do what I really wanted. perhaps that's just not practical enough these days, when my goals are really something else anyway.

when I took c++ in school, years ago, we never covered vectors. I learned, or rather, got introduced to arrays, and pointers. I never used c++ like I wanted since then. So now I'm teaching myself, and I go on what I remember.

RTFM? I take it that means "read the (insert explicative) manual" well I do search out the internet and attempt tp figure things out myself before I post questions. I still looking for good sources. Thanks for your link. I've been reading it and learning alot. I had read something that led me to believe that what I was doing was ok.

after banging my head on this program for a while now, and it becoming more tangled, I'm starting over, this time using vectors and classes in this manner that I obviously never learned.

if anyone can post some good resources that any c++ programmer should know, and that would be useful to any "beginning" programmer who needs to write some fairly complex code, I'd really appreciate that.

thanks
james

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement