donkeyKong
donkeyKong

Reputation: 13

Cannot find problem - [Error] ld returned 1 exit status

I need help finding this error the compiler is giving me:

C:\Users\AppData\Local\Temp\ccaruUld.o Test3_Problem2.cpp:(.text+0x73): undefined reference to `SUM(float, float)'

C:\Users\Downloads\collect2.exe [Error] ld returned 1 exit status

Code:

    //Test 3 - Problem 2
#include<iostream>
#include<cmath>
using namespace std;

float SUM(float value,float sum);

int main()
{
    //Introduce program to user
    cout<<"This program asks for 5 numbers and averages the numbers \
together.";
    
    //Declare variables
    float averg,value[4],sum1=0;
    
    //Allow user to enter 5 values
    for(int i=0;i<=4;i++){
        cin>>value[i];
    }
    sum1=SUM(value[4],sum1);
    cout<<value[0]; 
    
}
    float SUM(float vales[4],float sum) {
        for(int i=0;i<=4;i++){
            sum+=vales[i];
    }
    return sum;
}
    

Cannot whatsoever find it no matter how much I fix my code. I looked at similair questions and since this is an entry level C++ course all of the answers looked like alien language to me...

Upvotes: 0

Views: 4094

Answers (3)

Guilherme Cunha
Guilherme Cunha

Reputation: 49

I encountered a few different problems, so I rewrote the code and commented on the errors that I fixed. Hope this helps!

//Test 3 - Problem 2
#include<iostream>
#include<cmath>
using namespace std;

//THE FIRST PARAM WAS NOT DECLARED AS A VECTOR
//obs: That way you do not need to set the vector size limit or the variable name
float SUM(float[],float);

int main()
{
    //Introduce program to user
    //Forgot the " << endl;"
    cout<<"This program asks for 5 numbers and averages the numbers \ together." << endl;

    //Declare variables
    //Declared vector size less than 5 numbers
    float averg,value[5],sum1=0;

    //Allow user to enter 5 values
    for(int i=0;i<=4;i++){
        cin >> value[i];
    }
    sum1 = SUM(value,sum1);
    //You were print the vector at position 0 instead of the result of the sum
    //and forgot the " << endl;"
    cout << sum1 << endl;; 

}
    //VALES WAS NOT DECLARED AS A VECTOR
    //obs: Beware of indentation
float SUM(float vales[],float sum) {
    for(int i=0;i<=4;i++){
        sum+=vales[i];
    }
    return sum;
}

Upvotes: 2

10ZKhaled
10ZKhaled

Reputation: 154

Try this, it should work:

//Test 3 - Problem 2
#include<iostream>
#include<cmath>
using namespace std;

float SUM(float vales[4],float sum) {
    for(int i=0;i<=4;i++){
        sum+=vales[i];
}
return sum;
}

int main()
{
 //Introduce program to user
 cout<<"This program asks for 5 numbers and averages the numbers \
 together.";

//Declare variables
float averg, value[4], sum1=0.0;

//Allow user to enter 5 values
for(int i=0;i<=4;i++){
    cin>>value[i];
}
sum1=SUM(value,sum1);
cout<<value[0];
}

Upvotes: 0

AdaRaider
AdaRaider

Reputation: 1136

Assuming the code you have posted here is all of it, your problem is that you have not implemented the SUM() function.

This code:

float SUM(float value,float sum);

Is called a function prototype which is not the same as an implementation of that function.

https://en.wikipedia.org/wiki/Function_prototype

The term function prototype is particularly used in the context of the programming languages C and C++ where placing forward declarations of functions in header files allows for splitting a program into translation units, i.e. into parts that a compiler can separately translate into object files, to be combined by a linker into an executable or a library.

This is why your IDE is possibly not warning you that the SUM function does not exist, because you have a function prototype at the top of your code which matches the calls in your code to that function.

To fix this you need to implement the SUM function:

float SUM(float value,float sum)
{
    //Implementation
}

Upvotes: 1

Related Questions