Sign in to follow this  
squareball

[Solved]C++ error: expected constant expression

Recommended Posts

squareball    122
In my current homework assignment, I'm supposed to use code from my textbook. Unfortunately, I'm growing more and more upset with this book's code, since I've been finding all sorts of problems with it, including (not limited to) something declared with new that never was deleted (the code makes no mention of its problems, so I'm pretty sure it's due to the author's negligence). Anyway, I'm getting the following errors, all referring to the same line of code: error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 error C2133: 'str' : unknown size This occurs for the following code segment:
...
}

const char * headerStr = "Variable";
const int headerSize = strlen(headerStr);

int VariableLengthBuffer::ReadHeader(istream & stream)
// read the header and check for consistency
{
	char str[headerSize+1];
...




The error refers to char str[headerSize+1];, which might make sense except that it refers to a constant int, which refers to the length of a constant string. My compiler is VC++ 2005. Any help would be appreciated. [Edited by - squareball on October 9, 2007 6:16:56 PM]

Share this post


Link to post
Share on other sites
CmpDev    100
Quote:
My compiler is VC++ 2005.

This is the problem and I suspect the book is using g++ which has an extension which allows this. Anyway it is not standard C++ to be able to define a stack array with a runtime variable, instead you will need to new and delete the array on the heap, or use a vector.

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
Quote:
Original post by squareball
The error refers to char str[headerSize+1];, which might make sense except that it refers to a constant int, which refers to the length of a constant string.


Statically allocated arrays need to have a known length at compile-time. Even though headerSize uses the const keyword, is it a run-time constant (that is, it can be initialized to any value at run-time, but once initialized it remains constant).

You could either make headerSize a static const int, so it is constant at compile time, or dynamically allocate the array with new.

Or, better yet, use a std::vector<char>, or, if this is supposed to represent a logical string, std::string.

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
Quote:
Original post by squareball
I tried setting the variable as a static const, but that didn't work. I then used new to declare it, and it worked.

Thanks to both of you.


Don't forget that if you are newing the array you'll need to delete it when you are done with it (and similarly delete[] what you new[]). I still think you should be using either a std::vector<char> or std::string, but as long as it's working, have fun.

Share this post


Link to post
Share on other sites
Ezbez    1164
Quote:
Original post by CmpDev
Quote:
My compiler is VC++ 2005.

This is the problem and I suspect the book is using g++ which has an extension which allows this. Anyway it is not standard C++ to be able to define a stack array with a runtime variable, instead you will need to new and delete the array on the heap, or use a vector.


If that is the case, then either A)the book isn't actually teaching C++, or B)the book should have made this fact extremely obvious and unmissable.

For the sake of Google searches and what-not, what was the book that this came from?

And as an aside, static const int doesn't work since static variables cannot be assigned in their initializer to the result of a function call, I'm pretty sure. I remember having an issue about that before. You can, of course, assign them at run-time (unless they're const like this one) to the result of a function, but that defeats the purpose of this. It wouldn't make sense to run a function (even one with such an obvious result as strlen( "some_set_text" )) at compile-time.

Share this post


Link to post
Share on other sites
squareball    122
Quote:
Original post by Ezbez
If that is the case, then either A)the book isn't actually teaching C++, or B)the book should have made this fact extremely obvious and unmissable.

For the sake of Google searches and what-not, what was the book that this came from?

File Structures
An Object-Oriented Approach with C++
Authors: Michael J. Folk, Bill Zoellick, Greg Riccardi

On the page showing copyrights and such: Reprinted with corrections, March 1998


The book tries to be neutral toward file structures to an extent, but is heavily biased not only toward C++ (as the title implies), but toward Unix as well.

Share this post


Link to post
Share on other sites
MaulingMonkey    1728
Quote:
Original post by squareball
On the page showing copyrights and such: Reprinted with corrections, March 1998


OLD

C++ wasn't even first standardized until later that year. It's that old. Since this is the correction, this means that the original version is even older.

As such, it's a bit understandable that your book freakin' sucks.
It's also a bit understandable that you dislike the fact that it freakin' sucks.
You may wish to point out the fact that it freakin' sucks to your instructor.
Because, damn, I freakin' want that vacuum.

[Edited by - MaulingMonkey on October 10, 2007 10:01:44 PM]

Share this post


Link to post
Share on other sites
Zahlman    1682
But not only is it old, it was wrong even in its own time, after corrections.

Not only is it

Quote:
not standard C++ to be able to define a stack array with a runtime variable


, but it has never been standard C++, and was not even standard C until 1999 (and even today, the C99 standard seems not to be in widespread use compared to C89, for whatever reason).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this