Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, error invalid conversion from `int*' to `int'

Tags:

c++

I have the following C++ code:

#include <iostream>
using namespace std;
int main(){
}
int findH(int positionH[]){
    return positionH;         //error happens here.
}

The compiler throws an error:

invalid conversion from `int*' to `int'

What does this error mean?

like image 546
user1876088 Avatar asked Dec 04 '12 15:12

user1876088


2 Answers

positionH[] is an array, and its return type is int.

The compiler will not let you do that. Either make the parameter an int:

int findH(int positionH){
    return positionH;        
}

Or make the return type a pointer to an int:

int* findH(int positionH[]){
    return positionH;        
}

Or convert the array to an integer before return:

int findH(int positionH[]){
    return positionH[0];
}
like image 125
Pavenhimself Avatar answered Sep 28 '22 00:09

Pavenhimself


This line is invalid C++ (and invalid C too, which your code appears to be written in):

int bla[2] = findH(field, positionH);

bla is an array of 2 elements and cannot be initialised that way. findH returns int.

like image 22
CashCow Avatar answered Sep 28 '22 00:09

CashCow