Why am I Getting an Error Converting a 'Float**' to 'Const Float**'

Why am I getting an error converting a ‘float**’ to ‘const float**’?

See Why am I getting an error converting a Foo** → const Foo**?

Because converting Foo**const Foo** would be invalid and dangerous ... The reason the conversion from Foo**const Foo** is dangerous is that it would let you silently and accidentally modify a const Foo object without a cast

The reference goes on to give an example of how such an implicit conversion could allow me one to modify a const object without a cast.

error: cannot convert ‘float’ to ‘float*’ for argument

void calc_fun(float[], float[], float[], float[], float[], float[], float[], float[], float[], float[], const int);
// scroll right ^^^^^^^

You've declared this argument to be a pointer.

float /* snip */
total_gross = 0,

You've declared total_gross to be a float. It is not an array, and it is not a pointer.

calc_fun(rate, hrs_worked, gross, overtime, state_tax,fed_tax, uni_fee, net, total_gross, avg_gross, SIZE);
// scroll right ^^^^^^^^^^^

You pass total_gross as an argument that you declared to be a pointer. The types do not match.

invalid conversion from 'const float*' to 'float*' in c++

You declared m_ErrVal as a const float so &m_ErrVal is a const float *. The constructor of Mux expects a float * not a const float *. This is the error.

If you really want to do this, you will have to use const_cast.

For example const_cast<float*>(&m_ErrVal). Or you can remove the const qualifier if not needed, it is yours to choose.


EDIT: But if you do not intend to modify the given float * to the Mux constructor, I think you should pass them as const float * instead of using const_cast.

For more information about the use of const_cast, you can read @MaxLanghof comment or check this link.

Cannot convert 'float' to 'float*' for argument '1'

First of all, note that main(void){ is not a valid signature for main.

It should be:

int main(void){

Then:

float vett[10];
vett[10] = 0;

This is not valid. Array indices start at 0, so index 10 is out of bounds, as it would require an array with size 11.

Also, as your average function takes as first argument a float array, you'll need to pass it this way:

average(vett,media);

Using:

average(&vett[10],media);

Will pass a pointer to the data located right after the array, so obviously you'll get junk values.

I keep getting the error cannot convert 'float*' to 'float' in return

Unfortunately, C-style arrays aren't first-class objects in C++, which means you can't easily return them from a function the same way you would other object types. There are ways around that limitation, but they are awkward; the best approach for C++ is to define an object-type instead, like this:

#include <math.h>
#include <array>
#include <iostream>

// Let's define "Point3D" to be an array of 3 floating-point values
typedef std::array<float, 3> Point3D;

Point3D CartesianToCylindrical (const Point3D & pos)
{
//pos is in the form of [x,y,z]//
Point3D cylpos;
cylpos[0] = sqrt((pos[0] * pos[0]) + (pos[1] * pos[1]));
cylpos[1] = atan(pos[1] / pos[0]);
cylpos[2] = pos[2];
return cylpos;
}

int main(int, char **)
{
const Point3D p = {1,2,3};
const Point3D cp = CartesianToCylindrical(p);
std::cout << "(x,y,z) = " << cp[0] << ", " << cp[1] << ", " << cp[2] << std::endl;
}

.... that way you can pass and return your point-values in a natural manner.

Code Keeps saying: [Error] cannot convert 'float*' to 'float' for argument '1' to 'void test(float, int)'

Your function prototype void test(float, int); does not match your function void test(float a[], int size). Change the prototype at the top to void test(float a[], int size); (I like to leave the input variable names in the prototype for consistency, but this is not necessary).



Related Topics



Leave a reply



Submit