Problems with consts and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xillez
    New Member
    • Jul 2013
    • 93

    Problems with consts and arrays

    Hey.

    I'm using VC++

    Problem: "antPoints must be a const value".

    Code:
    #include <SFML/Graphics.hpp>
    #include <math.h>
    
    const int antPoints = pow(2, detail + 1) + 1;
    sf::Vector2f points[antPoints];			// My points to draw later.
    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?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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)*(det ail+1)+1.

    However, what is detail and what is its value?

    Comment

    • Xillez
      New Member
      • Jul 2013
      • 93

      #3
      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...

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        Working...