"expect a constant expression"

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 52
Reputation: ShadowScripter is an unknown quantity at this point 
Solved Threads: 0
ShadowScripter's Avatar
ShadowScripter ShadowScripter is offline Offline
Junior Poster in Training

"expect a constant expression"

 
0
  #1
Apr 17th, 2009
Bah, once again I am beat by the computer logic...
Anyone have an answer as to why it gives me an error: "Expected a constant expression at msg"

Here's the code,

  1. const int INFOWND_SIZE = 4;
  2. int msg_length[] = "Message";
  3.  
  4. double calc_amount = (double)msg_length/(double)INFOWND_SIZE;
  5.  
  6. int msg_amount = (int)ceil(calc_amount);
  7.  
  8. char msg[msg_amount][INFOWND_SIZE];

Anything unclear? eh.. let's take a look...
  • declaring a constant int with the value 4
  • divide length with size, giving a double
  • declare variable int with a cast (int) to make sure it calculates int, and then do the ceil()
  • declaring a char array with 2 dimensions, first one is the amount calculated, second is the size

Please try and give a simple answer that I can understand.
Constructive criticism, suggestions, anything that might optimize the code further is much appreciated, as long as you explain why.

//----------------------------------------------
Here's the whole code in context
  1. const int INFOWND_SIZE = 51;
  2.  
  3. void new_message(char var[]){
  4. COORD coord = {(infoffset.X+1), (infoffset.Y+1)};
  5. int real_infownd_size = INFOWND_SIZE-4;
  6. int msg_length = strlen(var);
  7. if( msg_length >real_infownd_size ){
  8. double calc_amount = (double)msg_length/(double)real_infownd_size;
  9. int msg_amount = (int)ceil(calc_amount);
  10. char msg[msg_amount][INFOWND_SIZE];
  11.  
  12. cout<<endl<<"length: "<<msg_length<<endl;
  13. cout<<"MAXIMUM: "<<INFOWND_SIZE-4<<endl;
  14. cout<<"amount of divs: "<<msg_amount<<endl;
  15. }
  16. }
This is a simple program I'm making, a sort of chat window, but not too complex yet. Anyway, at the start, I call the function with a parameter of 107. Rounded up it should be 3, the cout statement agreed, and it doesn't display a double value. Once I declare the variable named "msg_amount" as a const, it just piles up more errors...

The whole problem involves the section where I declare char msg[msg_amount][INFOWND_SIZE], where the compiler tells me it expected a const value. The whole idea of this is to take the string and divide it in sections so that the text won't overwrite other outputed stuff in the info window (infownd) since the window itself has a size.
Last edited by ShadowScripter; Apr 17th, 2009 at 8:24 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: "expect a constant expression"

 
0
  #2
Apr 17th, 2009
Does this code compile for you?

  1. int main(){
  2. size_t cols = 10;
  3. cin >> cols;
  4. size_t rows = 20;
  5. cin >> rows;
  6.  
  7. char x[cols][rows];
  8. for(int i = 0; i < rows; i++){
  9. for(int n = 0; n < cols; n++){
  10. cout << x[i][n];
  11. }
  12. cout << endl;
  13. }
  14.  
  15.  
  16. return 0;
  17. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 52
Reputation: ShadowScripter is an unknown quantity at this point 
Solved Threads: 0
ShadowScripter's Avatar
ShadowScripter ShadowScripter is offline Offline
Junior Poster in Training

Re: "expect a constant expression"

 
0
  #3
Apr 17th, 2009
Ah, no it doesn't. I don't think it likes it when you say size_t, what is that anyway? I've seen it before.

The problem is about why the compiler doesn't accept the integer when used in declaration of char array.

here's a functional code snippet

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <string.h>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. const int INFOWND_SIZE = 51;
  12.  
  13. void new_message(char var[]){
  14. int real_infownd_size = INFOWND_SIZE-4;
  15. int msg_length = strlen(var);
  16. if( msg_length >real_infownd_size ){
  17. double calc_amount = (double)msg_length/(double)real_infownd_size;
  18. int msg_amount = (int)ceil(calc_amount);
  19. //char msg[msg_amount][INFOWND_SIZE];
  20.  
  21. cout<<endl<<"length: "<<msg_length<<endl;
  22. cout<<"MAXIMUM: "<<INFOWND_SIZE-4<<endl;
  23. cout<<"amount of divs: "<<msg_amount<<endl;
  24. }
  25. }
  26.  
  27. int main(){
  28. new_message( "thisshouldbeapproxmorethan107charactersandwhendividedwithwindowsizeitshouldgiveapproximatll3atround");
  29. return 0;
  30. }

EDIT: Oh, forgot to use std::, on that thing, anyway, I implemented it in my function and nothing changed.

Here's the error messages:
1>(src)(122) : error C2057: expected constant expression
1>(src)(122) : error C2466: cannot allocate an array of constant size 0
1>(src)(122) : error C2133: 'msg' : unknown size
Last edited by ShadowScripter; Apr 17th, 2009 at 8:52 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: "expect a constant expression"

 
0
  #4
Apr 17th, 2009
... size_t is a standard type (~100% of the time it's an unsigned int)... that's weird. Could you copy and paste what your compiler is saying please?

size_t is:
http://www.cplusplus.com/reference/c...string/size_t/
Last edited by Clockowl; Apr 17th, 2009 at 8:32 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: "expect a constant expression"

 
0
  #5
Apr 17th, 2009
What archaic compiler are you using? Hehe. Get GCC, it supports (practically) all the C++ features + a bit more.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 52
Reputation: ShadowScripter is an unknown quantity at this point 
Solved Threads: 0
ShadowScripter's Avatar
ShadowScripter ShadowScripter is offline Offline
Junior Poster in Training

Re: "expect a constant expression"

 
0
  #6
Apr 17th, 2009
Pff! I use the one Visual C++ Express offer, so far worked perfectly...

Also, it allowed size_t but the errors remained the same

"size_t msg_amount = (int)ceil(calc_amount);"

THE PROBLEM IS WHY DOESN'T THE CHAR ARRAY TAKE MY NEWLY EVALUATED INT VARIABLE?!?!!??! XD
Last edited by ShadowScripter; Apr 17th, 2009 at 8:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 52
Reputation: ShadowScripter is an unknown quantity at this point 
Solved Threads: 0
ShadowScripter's Avatar
ShadowScripter ShadowScripter is offline Offline
Junior Poster in Training

Re: "expect a constant expression"

 
0
  #7
Apr 17th, 2009
Still waiting for an answer, idea, suggestion... anything that might explain these errors. See above posts.

It doesn't make sense to me. I pass an int inside the char array like so:

double whatever = (double)bla/(double)bla2;
int int_whatever = (int)ceil(whatever);
char stringster[int_whatever][80];
Last edited by ShadowScripter; Apr 17th, 2009 at 9:10 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: "expect a constant expression"

 
0
  #8
Apr 17th, 2009
I know that's the problem, and MSVC++ is sometimes quite archaic when it comes to "C code". You can try and use the vector class? MSVC++ apparently can't create 2D "standard" arrays dynamically.

You can of course create the array yourself with new, see:
http://www.parashift.com/c++-faq-lit...html#faq-16.16

And bookmark that website. Lots of useful info.
Last edited by Clockowl; Apr 17th, 2009 at 9:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 52
Reputation: ShadowScripter is an unknown quantity at this point 
Solved Threads: 0
ShadowScripter's Avatar
ShadowScripter ShadowScripter is offline Offline
Junior Poster in Training

Re: "expect a constant expression"

 
0
  #9
Apr 17th, 2009
So far, everything is very confusing, but the code looks awfully similar to the one you posted earlier...
So, basically what I'm trying to say is, is there no easy way to do what I want it to do?
maybe I can use a pointer or something...

what is this vector class you mentioned?
Last edited by ShadowScripter; Apr 17th, 2009 at 9:30 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: "expect a constant expression"

 
0
  #10
Apr 17th, 2009
Originally Posted by ShadowScripter View Post
what is this vector class you mentioned?
Short version: STL's resizable arrays.

Long version: I hope you realize I'm not superior to www.cplusplus.com or any reference on the STL when it comes to questions like this right?

If you get stuck on vectors, create a new thread about vectors, but I don't think you'll get stuck on it. It's a great and easy to use class.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC