On assignment, I need to write Sourse file.cpp, and write functions into it. What I actually did. But when compiling, I get an error: How to fix this?

* 1>functions.obj : error LNK2005: "void __cdecl arr_input(int *,int)" (?arr_input@@YAXPAHH@Z) уже определен в Functions c++ mass.obj 1>functions.obj : error LNK2005: "int __cdecl arr_max(int *,int)" (?arr_max@@YAHPAHH@Z) уже определен в Functions c++ mass.obj 1>functions.obj : error LNK2005: "int __cdecl arr_min(int *,int)" (?arr_min@@YAHPAHH@Z) уже определен в Functions c++ mass.obj 1>functions.obj : error LNK2005: "void __cdecl arr_out(int *,int)" (?arr_out@@YAXPAHH@Z) уже определен в Functions c++ mass.obj 1>functions.obj : error LNK2005: "void __cdecl arr_reverse(int *,int)" (?arr_reverse@@YAXPAHH@Z) уже определен в Functions c++ mass.obj 1>functions.obj : error LNK2005: "int __cdecl arr_sum(int *,int)" (?arr_sum@@YAHPAHH@Z) уже определен в Functions c++ mass.obj 1>C:\Users\Galya\Documents\Visual Studio 2017\Projects\Functions c++ mass\Debug\Functions c++ mass.exe : fatal error LNK1169: обнаружен многократно определенный символ - один или более * 

Here is the code for the main file.

 // Functions c++ mass.cpp: определяет точку входа для консольного приложения. // #include "stdafx.h" #include<iostream> #include"functions.cpp" using namespace std; int main() { const int m = 5; int arr[m]; arr_input(arr,m); arr_out(arr, m); return 0; } 

// code functions.cpp

 #include "stdafx.h" #include <iostream> using namespace std; void arr_out(int *a,int n) { for (int i = 0; i < n; i++) { cout << a[i] << " "; } } void arr_input(int*a,int n) { for (int i = 0; i<n; i++) cin >> a[i]; } int arr_sum(int*a, int n) { int sum = 0; for (int i = 0; i < n; i++) sum += a[i]; return sum; } int arr_max(int*a, int n) { int maxArr; maxArr = a[0]; for (int i = 0; i < n; i++) { if (a[i]>maxArr) { maxArr = a[i]; } } return maxArr; } int arr_min(int*a, int n) { int minArr; minArr = a[0]; for (int i = 0; i < n; i++) { if (a[i]>minArr) { minArr = a[i]; } } return minArr; } void arr_reverse(int*a, int n) { for (int i = 0; i < n; i++) { swap(a[i], a[n - i]); } for (int i = 0; i < n; i++) { cout << a[i] << " "; } } 
  • one
    Send our greetings to Gale! - VladD
  • @ VladD, lol, be sure to convey) - Awesome Man

2 answers 2

1) It is common to use header file (.h / .hpp) as a file with functions. Try to create this file, write these functions, and connect it. 2) If that doesn't help, try removing stdafx.h

  • 2
    If this did not help, then take a calming one. :) - Vlad from Moscow

There are two compilation units in your project: the main one with the main function and the functions.cpp module. Since you included the functions.cpp module in the main module, these function definitions appear in the project twice.

You should declare all your user-defined functions in the header file, for example, in the functions.h file. This header file is included in the main module and in the functions.cpp module where these functions will be defined using the directive #include . And the module functions.cpp will not be included anywhere with the help of the #include directive.

  • one
    Thank you, but if I throw everything in the header, what should I write in functions.cpp , or is it so for reference =)? - Awesome Man
  • one
    @AwesomeMan You can put everything in the header file, provided that this header file will not be included in other modules. That is, if you have one main file with main, and to it a header file with function definitions. Naturally functions.cpp in this case, you just need to remove from the project. - Vlad from Moscow