473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problems with consts and arrays

93 64KB
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.

3 1075
donbock
2,426 Expert 2GB
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
Xillez
93 64KB
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
9,208 Expert Mod 8TB
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

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

Similar topics

7
by: Old Lady | last post by:
Hi all, I have a problem when I try to send an array using a form when the type="checkbox". This is my form input row: <INPUT type="Checkbox" name="flg" value="y" <? if($row == 'y') echo...
0
by: mb3242 | last post by:
Hello, I'm trying to use win32com to call a method in a COM object. I'm having a problem with Python choosing the wrong type when wrapping up lists into VARIANTs. The function I'm trying to call...
4
by: Wayne Wengert | last post by:
I am still stuck trying to create a Class to use for exporting and importing array data to/from XML. The format of the XML that I want to import/export is shown below as is the Class and the code I...
1
by: hledman | last post by:
Hello, Beginner here reading through murach's c# book and come to a point where the book doesn't give a good example of what they want you to do in the exercise. I've created an array with 5...
5
by: Alan Howard | last post by:
We're getting "ERROR (0x8007000E) Not enough storage is available to complete this operation" errors on a fairly large, busy ASP/SQL Server web site. The error is being thrown on a line calling...
2
by: Martien van Wanrooij | last post by:
I am working on some financial calculators and although I succeeded to created the required formulas I am not sure about the following.To give an example: when somebody puts a capital on the bank...
7
by: virkof | last post by:
Hi everyone, I am trying to do this projects which contains two different clases. The first class called Line, is an empty array of 6. The second one called, LineArray is the one in charge of...
2
BSCode266
by: BSCode266 | last post by:
Hi, This is some code in the first class: //the array of pointers Slot *slots; //the get method for the previous array //The first error occurs here: error C2440: 'return' : cannot...
3
by: O.B. | last post by:
Below is a program that shows a test for marshaling data from a byte array to a class structure. Unfortunately, there are two annoying problems (bugs?) that I can't seem to get around. The...
9
by: Emmash | last post by:
Hi, I'm having a problem with array's functions //Making the list of the differents arrays if($cpt_couleur+1<count($_POST)){...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.