error invalid conversion from 'float*' to 'int' [-fpermissive] - c++

Ok so, i'm trying to covnert some number into others via function. The vector "tempc" contains 7 celsius numbers. The type "tempf" will contain fahrenheit. But i get this error in the title "error invalid conversion from 'float*' to 'int' [-fpermissive]". I'm new to programming , so i'm kinda really newbie. Sorry for the code but it's in italian
#include <iostream>
using namespace std;
//dichiarazione variabili e vettori
float tempc[7] = {23,25,22,19,18,20,16};
float tempf[7];
const int MAX_GRADI = 7;
//funzione conversione
int ConversioneInF ()
{
for (int i=0;i<MAX_GRADI;i++)
tempf[i]=(9/5)*tempc[i]+32; //C in F
return tempf;
}
int main ()
{
ConversioneInF();
for (int i=0;i<MAX_GRADI;i++)
cout<<"La temperatura in fahrenheit e' :"<<tempf[i]<<endl;
return 0;
}

In your function ConversioneInf you are returning tempf.
This is a float-array, which in c++ is a pointer to float (float*)
This type cannot be converted to the specified return type int.
As you are not using the return value, just change it to void:
void ConversioneInF ()

void ConversioneInF ()
{
for (int i=0;i<MAX_GRADI;i++)
tempf[i]=(9.0/5.0)*tempc[i]+32;
}
Anyway, tempf is global, you don't need to return it.

Related

I am getting error like [Error] cannot convert '__complex__ int' to 'int' in assignment

here is my code and I amgetting error cannot convert 'complex int' to 'int' in assignment
#include<iostream>
using namespace std;
int main() {
// Write your code here
int x;
cin>>x;
int res=0;
for (int i=1;i<=5;i++){
res = 3i+2;
}
cout <<res;
}
Your code is syntactically incorrect.
I assume the correct code is this-->
#include<iostream>
using namespace std;
int main() {
// Write your code here
int x;
cin>>x;
int res=0;
for (int i=1;i<=5;i++){
res = 3*i+2;
}
cout <<res;
}
I actually don't understand what are you trying to do but res=3i+2 is wrong as you should write res=3*i+2 to multiply if that's what you are trying to do

Error error: ‘void*’ is not a pointer-to-object type

Hello Guys I am facing a slight issue in a code
This program is supposed to calculate
( (a+b) x (c+d) ) / e
Create three threads, one for
Addition Only
Multiplication Only
Division
ONLY one thread should be created in main()
Display the results in division thread.
The int values(a, b, c, d, e) should be taken from the user in main and passed on the thread created in main. The other result of each step should be passed on the next step.
And this is the program that i wrote for above scenario
#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void *Division(void *arg_div)
{
int result = arg_div[0]/arg_div[1];
cout << result ;
pthread_exit(NULL);
}
void *Multiplication(void *arg_mul)
{
int arg1[2];
arg1[0]=arg_mul[0]*arg_mul[1];
arg1[1]=arg_mul[2];
pthread_t div;
pthread_create(&div,NULL,Division,(void*)arg1);
pthread_join(div,NULL);
pthread_exit(NULL);
}
void *Addition(void *arg_add)
{
int arg[3];
arg[0]=arg_add[0]+arg_add[1];
arg[1]=arg_add[2]+arg_add[3];
arg[2]=arg_add[4];
pthread_t ad;
pthread_create(&ad,NULL,Multiplication,(void*)arg);
pthread_join(ad,NULL);
pthread_exit(NULL);
}
int main()
{
int values[5];
for(int i=0;i<5;i++)
{
cin >> values[i];
}
pthread_t pa;
pthread_create(&pa,NULL,Addition,(void*)values);
pthread_join(pa,NULL);
return 0;
}
Why did you change the pointer type (C-style...) to void*? (trick question to make you walk on the proper path).
Just get the int* back, they are pointers that you can dereference. void* are pointers, but you can't get a void object and even less do operations on it.
int* args = static_cast<int*>(arg);
arithmetic on void pointer is not allowed, you need to convert void * to target type here i.e. int*
after converting to void* to int*, your Addition() looks like below, you have to make same changes to all function to make it work.
void *Addition(void *arg_add)
{
int *input = (int *)arg_add;
int arg[3];
arg[0]=input[0]+input[1];
arg[1]=input[2]+input[3];
arg[2]=input[4];
}
You can't dereference a void* - you need to cast the thread functions' parameters back to int*.
For example,
void *Division(void *arg_div)
{
int* args = static_cast<int*>(arg_div);
int result = args[0]/args[1];
...
void * Division(void * arg_div) {
int * arg_div_int = (int * ) arg_div;
int result = arg_div_int[0] / arg_div_int[1];
...
}
void * Multiplication(void * arg_mul) {
int * arg_mul_int = (int * ) arg_mul;
int arg1[2];
arg1[0] = arg_mul_int[0] * arg_mul_int[1];
arg1[1] = arg_mul_int[2];
...
}
void * Addition(void * arg_add) {
int * arg_add_int = (int * ) arg_add;
int arg[3];
arg[0] = arg_add_int[0] + arg_add_int[1];
arg[1] = arg_add_int[2] + arg_add_int[3];
arg[2] = arg_add_int[4];
..
}
Here is a fix to your problem. You have to convert the void* to a int* and then use the values in the int array.

RNB Calculator using a Linked list

include <iostream>
#include <string.h>
using namespace std;
#include "Stack.h"
int main()
{
Stack<float>b;
Stack <float> a(b);
char num[100];
while (true)
{
cout << "Enter: " << endl;
cin >> num;
if(strcmp(num,"Q") == 0 ||strcmp(num,"q")==0)
break;
if(strcmp(num,"*")==0)
{
float num1 = b.pop();//Error: Cannot initialize a variable of type 'float' with an rvalue of type 'void'
float num2 = b.pop();// Error: Cannot initialize a variable of type 'float' with an rvalue of type 'void'
if(num1 && num2){
a.push(num1*num2);
}
This I a part of my program. the problem is that I need to store the value I pop from my list and then multiply them and store them back. Over here I am trying to store the values in float number so that I can do an operation but it gives me an error as stated above any help will be appreciated.
If your "Stack.h" is similar to <stack>:
Referring to Stack.pop():
Return value
none
So you should use Stack.top():
Return value
A reference to the top element in the stack.
Sample Code
float num = b.top(); // get top value of stack
b.pop(); // pop it, not used anymore
// do something with num here

cannot convert 'double(_cdecl*)()' to 'double'

As an assignment I have to write a code that takes user inputs, performs an operation with them, then prints them to the screen. However, I keep getting an error on line 18 where I call FunctionMultiply saying that the function cannot convert 'double(_cdecl*)()' to 'double'. I searched for this type of problem but it seems like all of them have to do with arrays which aren't in my code. How can I fix this?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int GetInt(void);
double GetDouble();
char GetLetter(void);
double FunctionMultiply(int, double);
int FunctionCharacter(char);
int main()
{
GetInt();
GetDouble();
GetLetter();
FunctionMultiply(GetInt, GetDouble);
FunctionCharacter(GetLetter);
printf("%f", FunctionMultiply);
printf("%c", FunctionCharacter);
return 0;
}
int GetInt(void)
{
int integer;
printf("Enter an integer\n");
scanf("%i", &integer);
return integer;
}
double GetDouble()
{
double dub;
printf("Enter a floating point number\n");
scanf(" %lf", &dub);
return dub;
}
char GetLetter(void)
{
char letter;
printf("Enter a letter\n");
scanf(" %c", &letter);
return letter;
}
double FunctionMultiply(int arg1, double arg2)
{
double product = arg1 * arg2;
return product;
}
int FunctionCharacter(char letter)
{
if (toupper(letter) <= 'M')
{
return 0;
}
else
{
return 1;
}
}
I think you are confusing function identifiers with storage. You have just called a bunch of functions at the beginning and not stored their results in anything.
It appears that you expect using the function identifier on its own will give you the result of the last call to that function. But it does not.
Here is how you would store the return values and use them later:
int my_int = GetInt();
double my_double = GetDouble();
char my_char = GetLetter();
double multiply_result = FunctionMultiply( my_int, my_double );
char char_result = FunctionCharacter( my_char );
printf( "%f", multiply_result );
printf( "%c", char_result );
Modify your main() like this:
int main()
{
int i = GetInt();
double d = GetDouble();
char c = GetLetter();
double a = FunctionMultiply(i, d);
char c = FunctionCharacter(c);
printf("%f", a);
printf("%c", c);
return 0;
}
Your problem is that you are passing function names rather than calling them. i.e. GetInt instead of GetInt().
It looks like you weren't paying attention to the lessons or examples showing how to use functions.
GetInt();
This calls the GetInt function, and ignores its return value.
GetDouble();
This calls the GetDouble function, and ignores its return value.
GetLetter();
This calls the GetLetter function, and ... you know the score by now.
FunctionMultiply(GetInt, GetDouble);
This is just nonsense. You're trying to call the FunctionMultiply function, passing the functions GetInt and GetDouble as arguments. You need to pass it an int and double, but you don't have an int and a double because you didn't store the results of GetInt and GetDouble anywhere.
You should have done this:
int i = GetInt();
double d = GetDouble();
char l = GetLetter();
Now you have variables i, d and l that hold the results of those function calls, so you can pass them in to other functions:
FunctionCharacter(i, d);
You seem to be under the impression that the name of a function magically changes to the result of the call, after you've called the function once.
It doesn't. The function call expression itself is the result of the call.
Instead of
ReturnADouble();
// call ^ and value v somehow separated? Why did you ever think that?
double result = ReturnADouble;
But according to the language rules, ReturnADouble is still the name of a function, and the compiler righteously complains when you give the name of a function when you should be giving a numeric value.
Your code should read more like
double result = ReturnADouble();
// ^ this call results in a value

An error cannot convert from 'void *' to 'float *' `

I write a c++ function and its associated mex. But the one kind of input of c++ function is double *.
The output of function pointwise_search is a pointer. I was told that I should delete it. But I do not know where I should delete it since I need it as an output.
From the answer, I know I should check the type of input by mxIsSingle. So I correct the function mexFunction. But there is an error error C2440: '=' : cannot convert from 'void *' to 'float *'.
In matlab, I should call like pointwise_search(float *p,float q, num_thres,float n, len ). If I have a vector v_in_matlab=rand(5,1) in matlab. I should I get it pointer by p=single(v_in_matlab); and then pointwise_search(p...
Thanks in advance.
#include "mex.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
float * pointwise_search(float *p,float *q,int num_thres, float* n, int len )
{
vector<float> P(p, p + num_thres);
vector<float> Q(q, q + num_thres);
int size_of_threshold = P.size();
float *Y=new float[len];
float *z=new float[len];
typedef vector<float > ::iterator IntVectorIt ;
IntVectorIt start, end, it, location ;
start = P.begin() ; // location of first
// element of Numbers
end = P.end() ; // one past the location
// last element of Numbers
for (int i=0;i<len;i++)
{
location=lower_bound(start, end, n[i]) ;
z[i]=location - start;
if(z[i]>0&&z[i]<size_of_threshold)
{
Y[i]=(n[i]-P[z[i]])/(P[z[i]-1]-P[z[i]])*(Q[z[i]-1]-Q[z[i]])+Q[z[i]];
}
else
{
Y[i]=Q[z[i]];
}
}
return (&Y[0]);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
float * Numbers, *Q;
if (nrhs != 5)
{
mexErrMsgTxt("Input is wrong!");
}
float *n = (float*) mxGetData(prhs[3]);
int len = (int) mxGetScalar(prhs[4]);
int num_thres = (int) mxGetScalar(prhs[2]);
/* Input gs */
if(mxIsComplex(prhs[0])
||!mxIsSingle(prhs[0]))
mexErrMsgTxt("Input 0 should be a class Single");
/* get the pointer to gs */
Numbers=mxGetData(prhs[0]);
if(mxIsComplex(prhs[0])
||!mxIsSingle(prhs[0]))
mexErrMsgTxt("Input 0 should be a class Single");
/* get the pointer to gs */
Q=mxGetData(prhs[1]);
// float * Numbers= (float *)mxGetData(prhs[0]);
// float * Q= (float *)mxGetData(prhs[1]);
float * out= pointwise_search(Numbers,Q,num_thres,n,len );
//float* resizedDims = (float*)mxGetPr(out);
}
In Matlab use single() to convert the data before calling the mexFunction. On the C++ side verify that the type is indeed single by mxIsSingle(). After this you can happily cast to float*.
Before you worry about your MEX code, have another look at your C++ function first. You have some really obvious memory leaks (new but no delete[]).
Regarding MEX you should never see this:
(float *)mxGetPr(prhs[0])
You can't cast a double* to a float* and expect the numbers to make any sense. Input single from MATLAB and use:
(float *)mxGetData(prhs[0])
And do as Trilarion suggests and test all of your input mxArrays for the expected data type.