"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

Re: "expect a constant expression"

 
0
  #11
Apr 17th, 2009
You know, I just found out that IT IS POSSIBLE to do it my way, but it wont accept the way I calculate the "int"

I tested it with this:
  1. const int test1 = 10/5; //even number without having to round up
  2.  
  3. double test2_calc = (double)10/(double)5;
  4. const int test2 = (int)test2_calc;
  5.  
  6. char mychar[test1][ANOTHER_CONSTANT]; //Works
  7. char mychar[test2][ANOTHER_CONSTANT]; //Doesn't work

So, in light of this new information. How do I BEST CALCULATE that?

like, rounding up without having to convert to double?

I also think I need a lecture on casts, 'cause maybe I'm not handling them right...
Last edited by ShadowScripter; Apr 17th, 2009 at 10:01 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
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
  #12
Apr 18th, 2009
You can only do that when they are defined as constants, so MSVC++ can create the 2D arrays at compile time rather then at runtime.

I suggest reading how to make your own 2D arrays, or using vectors.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: "expect a constant expression"

 
0
  #13
Apr 19th, 2009
Why are you using old casting ?
Originally Posted by ShadowScripter View Post
double test2_calc = (double)10/(double)5;
const int test2 = (int)test2_calc;
It's a better practice to use the new casting operators to do conversion between datatypes:
double num = 5.23;
int num2 = static_cast<int>(num);
instead of the old ones:
double num = 5.23;
int num2 = (int)num;
Last edited by tux4life; Apr 19th, 2009 at 7:41 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
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
  #14
Apr 19th, 2009
See here why: http://www.informit.com/guides/conte...lus&seqNum=134

Boils down to C-style casts being static_casts or reinterpret_casts. But I never got reinterpret_cast to work anyway.
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
  #15
Apr 19th, 2009
See, now this is why I love these forums! You are all willing to share your knowledge (albeit most knowledge passed on to you by others as well). I didn't know about static_cast, because I rely most of my work on the C++ Beginner's Guide written by the Microsoft Corporation, I guess it's not up to date... either that or I missed static_cast entirely

Still tho, the answer to my original question remains unclear, well, I did get some suggestions from Clockowl about vectors, but they were much too confusing and I would ask you to explain them in a more easier way for me to understand, but that is for another thread.

At one time I thought about using pointers, during these 2 days since I posted my problem I have grown to learn more about the power of pointers. I managed to create a pretty good converter function, more specifically a Char To String, AND, a String to Char! I don't know if it already exists or is unnecessary, but it sure was fun!

Where was I?... right, thanks to the both of you, especially Clockowl! You seem very active. +rep (if my rep matters)
Last edited by ShadowScripter; Apr 19th, 2009 at 11:07 pm.
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