0

This is my code and I have the problem [Linker error] undefined reference to `Suma(int, int)'

#include<iostream>
#include<conio.h>
using namespace std;
int Suma(int,int);
int main()
{
    int n,x[1],i;
    cout<<"Numero de elementos del arreglo?:  ";
    cin>>n;
    for(i==0;i<n;i++)
    {
         cout<<"elemento "<<i+1<<":  ";
         cin>>x[i];
    }
    cout<<"La suma de los elementos es:  "<<Suma(n,x[1])<<endl;
    cout<<"<<El programa ah finalizado: codigo de salida: 0>>\n"; 
    cout<<"<<Presione enter para cerrar la ventana...>>";
    getch();
}
int Suma(int n, int x[])
{
     if(n==1) return x[1];
     return x[n-1]+Suma(n-1,x);
}

if I change this part Suma(n,x[1]) for Suma(n,x) the new error is "invalid conversion from int*' to int' " I don't know what's the problem and the result should be: enter image description here

  • 2
    You didn't even read the code one before posting? What type does `Suma()` take? What types are you passing? What types did you say it took? – John3136 Oct 11 '17 at 22:30
  • the code as well as the error is linker error and I don't know why but y think the problem is in "cout<<"La suma de los elementos es: "< – Héctor Alonso Oct 11 '17 at 22:46

2 Answers2

1

Brief description of errors:-

  • Linker error is thrown because the declared function is not found in the body to link. Linker here is trying to link the declartion to the address of the function and fails as its not found.
  • By changing the function declaration you have a new error because type conversion from int to array inside Suma() is failing. Either you accept int x and try to index x which is incorrect.

Detail -

Above code has following errors,

Declaration = int Suma(int, int) .

Defination = int Suma(int n, int x[]).

Code in Suma function => if(n==1) return x[1]; return x[n-1]+Suma(n-1,x);

Caller at main() => cout<<"La suma de los elementos es: "<<Suma(n,x[1])<<endl;

Look at the above lines. Suma function is declared as it accepts two integers. However at defination it accepting an array. Call to Suma function is before the function defination. So now its upto compiler to throw an error. Some might complain that there is a mismatch in declaration , and some might throw an error stating function not declared/ found.

Another issue the caller, Caller is passing x[1] either you are passing an integer not an array .

From all of the above it looks like you want to pass an Array - So the

  • Declaration should change to Array int Suma(int n, int x[]). So now compiler would match and hence linker should also find the function body for the declaration and hence no error.
  • Change main code just to call Suma(n, x) . Either cout<<"La suma de los elementos es: "<<Suma(n,x)<<endl;

This should work.

Seshadri VS
  • 550
  • 1
  • 6
  • 24
0

You prototype your function like this:

int Suma(int,int);

but implement it like this:

int Suma(int n, int x[])

int and int[] are not the same. Then in the code you do this:

Suma(n,x[1])

which matches the first declaration. Choose a prototype then implement it.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • how? I don't understand how, I'm new – Héctor Alonso Oct 11 '17 at 22:39
  • @HéctorAlonso this should well covered by whatever text you are learning from. If you have no text or the text does not cover this material, here is a list of texts that is generally agreed-upon as good: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list .Trying to learn any language without a good reference is a great way to waste your time. – user4581301 Oct 11 '17 at 23:03