Unable to call a function from int main() body

  • A+
Category:Languages

This is the SUM function I've created, to handle the arbitrary number of integers.

#include <iostream> using namespace std;  int sum(const int numbers[], const int numbersLen){     int sum = 0;     for (int i=0; i < numbersLen; ++i){         sum += numbers[i];     }      return sum; } 

I'm calling this function from the int main() but it keeps on giving the error

 [Error] invalid conversion from 'char' to 'const int*' [-fpermissive] 

The main function is as below:

int main(){     cout << sum([2],5);     return 0; } 

I know the mistake is very naive and small but help will be highly appreciated!

 


You need to send an array variable to the function as parameter. Try this..

int main(){     int numbers[] = {1,2,3,4,5};     cout << sum(numbers,5);     return 0; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: