0

i am trying to make a program that gave you sum of array elements absolute value .

this is the header file :

#include <iostream>
using std::cin,std::cout,std::endl;

#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions"
void getAbsSum(int arr[10]){
int abs = 0,sum = 0;
int len = *(&arr + 1) - arr;
for (int i = 0; i < len; ++i) {
    if (arr[i] < 0 ) {
        abs++;
        if (abs * *arr == *arr)
            arr[i] = abs;
        else
            continue;
    }
    else
        continue;
    sum+= *arr;
}
cout << sum;
} 
 #pragma clang diagnostic pop

this is the getAbsSum.cpp

#include "getAbsSum.h"
void getAbsSum(int arr[10]);

and this is the main.cpp

#include <iostream>
#include "getAbsSum.h"
using std::cin,std::cout,std::endl;
int main() {
int arr[10];
for (int & i : arr)
  cin >> i;
getAbsSum(arr);
return 0;
}

and this is the error :

    /home/mamog/CLionProjects/Absolute_sum/getAbsSum.h:6: multiple definition of `getAbsSum(int*)'; CMakeFiles/Absolute_sum.dir/main.cpp.o:/home/mamog/CLionProjects/Absolute_sum/getAbsSum.h:6: first defined here
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/Absolute_sum.dir/build.make:108: Absolute_sum] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Absolute_sum.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/Absolute_sum.dir/rule] Error 2
make: *** [Makefile:124: Absolute_sum] Error 2
moi
  • 467
  • 4
  • 19
MAMO GRAG
  • 9
  • 3
  • You seem to have switched around which things go in which files. Declarations go in header files, definitions go in source cpp files. Right now both `main.cpp` and `getAbsSum.cpp` see a definition for `getAbsSum`, which is a violation of the One Definition Rule. – Nathan Pierson Sep 05 '21 at 16:15
  • Off topic: Please read a good [C++ book](https://stackoverflow.com/a/388282/7010554). – maij Sep 10 '21 at 06:18

2 Answers2

2

Headers are substituted textually into every compilation unit where they are #include'd; as a result you have definitions of getAbsSum in both getAbsSum.cpp and main.cpp. This violates the one-definition rule.

The correct way to use a header is to include the declaration in the header and the definition in the cpp file; you have this backwards.

Alternatively, for small functions that don't have a lot of dependencies, you may choose to get rid of getAbsSum.cpp, mark the definition in the header as inline, and then anyone including your header will get a definition of the function.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
2

The .h and the .cpp files are inverted. The code should be in the .cpp, and the declaration, in the .h header.

getAbsSum.h:

void getAbsSum(int arr[10]);

getAbsSum.cpp

#include "getAbsSum.h"
#include <iostream>

using std::cin,std::cout,std::endl;

#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions"
void getAbsSum(int arr[10]){
int abs = 0,sum = 0;
int len = *(&arr + 1) - arr;
for (int i = 0; i < len; ++i) {
    if (arr[i] < 0 ) {
        abs++;
        if (abs * *arr == *arr)
            arr[i] = abs;
        else
            continue;
    }
    else
        continue;
    sum+= *arr;
}
cout << sum;
} 
 #pragma clang diagnostic pop

Iván
  • 971
  • 6
  • 20