By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

Problems with consts and arrays

P: 88
Hey.

I'm using VC++

Problem: "antPoints must be a const value".

Expand|Select|Wrap|Line Numbers
  1. #include <SFML/Graphics.hpp>
  2. #include <math.h>
  3.  
  4. const int antPoints = pow(2, detail + 1) + 1;
  5. sf::Vector2f points[antPoints];            // My points to draw later.
  6.  
Here I'm trying to find the number of points exactly I need to store so I don't use more space than I need (I know I could use a list or vectors but i havn't).

Here the antPoints excits and is set as a const and doesn't change when the points array comes and uses it right?

But why won't this work and what do I have to do to get it to work?
Apr 3 '17 #1

✓ answered by weaknessforcats

The pow function returns a double which is typecast to an int to make the initialization work.

Rule 1: You can never typecast between floating point and integer without truncation an loss of data.

If your compiler lets you do this without so much as a warning, get a different compiler.

In the example code, the contents of antPoints will be indeterminate.

Share this Question
Share on Google+
3 Replies


Expert 100+
P: 2,396
The antPoints initializer calls the pow() function. I'm not sure about C++, but C does not support function calls in initializers that are not defined within a function. You can replace the function call with an equivalent expression: (detail+1)*(detail+1)+1.

However, what is detail and what is its value?
Apr 3 '17 #2

P: 88
Detail is the detail level for a midpoint displacement algorithm for generating a 1d or 2d terrain surface.

So if the detail is 1 I find 1 dot in the middle.

If detail is 2 I find two dots on either side of the middle dot and so on...

Eventually it looks kinda like a horizon. In other words detail is the amount of times this algorithm is going to run and each time the detail of the terrain goes up.

Btw it works now, I switched from VC++ to MinGW c++ compiler instead and it works...
Apr 3 '17 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
The pow function returns a double which is typecast to an int to make the initialization work.

Rule 1: You can never typecast between floating point and integer without truncation an loss of data.

If your compiler lets you do this without so much as a warning, get a different compiler.

In the example code, the contents of antPoints will be indeterminate.
Apr 3 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.