943,648 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2031
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 17th, 2009
0

Re: "expect a constant expression"

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:
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
ShadowScripter is offline Offline
94 posts
since Apr 2009
Apr 18th, 2009
0

Re: "expect a constant expression"

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 19th, 2009
0

Re: "expect a constant expression"

Why are you using old casting ?
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 19th, 2009
0

Re: "expect a constant expression"

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 19th, 2009
0

Re: "expect a constant expression"

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.
Reputation Points: 12
Solved Threads: 6
Junior Poster in Training
ShadowScripter is offline Offline
94 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Major Problem with Doubly linked list program
Next Thread in C++ Forum Timeline: Trouble with multi-file program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC