fatal error LNK1169

Hi
I am receiving this error when i compile the program:
fatal error LNK1169: one or more multiply defined symbols found

Any idea about what cause it and how to fix it?
tnx in advance
Check the line above it in your output window. It should contain a symbol (variable name) that looks familiar. It means that you've defined that variable or object twice.

Since it's a linking error and not a compiling error, it probably has something to do with you defining something that is already defined in a header or something. Perhaps this is caused by using namespace std; and then using an object that is defined in another way in std::.
i think you mean this line:
error LNK2005: "void __cdecl sum_prod_vect(int * const,int)" (?sum_prod_vect@@YAXQAHH@Z) already defined in main.obj
Indeed i used "using namespace std;".What is the solution?
sum_prod_vect is this non-member function declared in a header file?
Where is the definition for this function?
Is this header file included in more than one file? Is this declared in a header file or is it implemented in a .cpp file only?

Maybe some code would help?
Last edited on
The program works when i use all the functions in the main file.It only gives me this error when i declare the function in another .cpp file and then use: #include "file_name.cpp" in the main file to include this function.
Last edited on
This is breaking the ODR rule - One Definition Rule. When you define the function in your .cpp "file_name.cpp" there exists a definition in that translation unit. Then when you #include "file_name.cpp" in your main.cpp you are textually including the definition again.

There are a few ways to work around this, 1 include the definition in your main. Another way, and I would prefer this myself, is to create a header file for "file_name" like "file_name.h", declare your function in this file, put a header guard in there, then in your file_name.cpp and main.cpp #include "file_name.h"
i have introduced a header with a header guard but i am still receiving the same error.
Here is the header and main file:
header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<conio.h>
#include<iostream>
using namespace std;


#ifndef HEADER_H
#define HEADER_H
typedef struct{
	int v[100];//elem vectorului
	int n;//dimensiunea vectorului
}VECTOR;

typedef struct{
	int a[100][100];//elem matricii
	int lin;//nr de linii
	int col;//nr de coloane
}MATRICE;

//operatii cu vectori
void cit_vect(int v[],int n);//citeste elemetentele unui vector
void print_vect(int v[],int n,char *m);//elem char al fct este folosit pt afisarea adevcata a numelui vectorului
void sum_prod_vect(int v[],int n);//calculeaza si returneaza suma si prod elem unui vector
void max_min_vect(int v[],int n);//Identifica si returneaza maximul si minimul dintre elem unui vector precum si poz acestora
void ord_cresc_descresc(int v[],int n,char *m);//elem char al fct este folosit pt afisarea adevcata a numelui vectorului
int suma_vectori(int v[],int u[],int n,int m);//calculeaza si afiseaza suma a doi vectori
int prod_vectori(int v[],int u[],int n,int m);//calculeaza si afiseaza produsul a doi vectori
int prod_scalar(int v[],int u[],int n,int m);//calculeaza si afiseaza produsul scalar a doi vectori
int prodv_col_lin(int a[][100],int b[][100],int lin,int col);//calculeaza si afiseaza produsul a doi vectori(un vector coloana si un vector linie)
//operatii cu matrici
void cit_mat(int a[100][100],int m,int n);//citeste elemetentele unei matrici
void print_mat(int a[100][100],int m,int n,char *t);//elem char al fct este folosit pt afisarea adevcata a numelui matricii
void suma_prod_mat(int a[100][100],int m,int n);//calculeaza si returneaza suma si prod elem unei matrici
void max_min_mat(int a[100][100],int m,int n);//Identifica si returneaza maximul si minimul dintre elem unei matrici precum si poz acestora
void el_poz_neg_nul(int a[100][100],int m,int n);//returneaza nr de elem poz,neg si nule ale unei matrici
void suma_el_diag(int a[100][100],int m,int n);//returneaza suma elem de pe diag principala ale unei matrici
int  suma_mat(int a[100][100],int b[100][100],int m,int n,int k,int l);//returneaza suma a doua matrici
int prod_mat(int a[100][100],int b[100][100],int m,int n,int k,int l);//returneaza suma a doua matrici

#endif 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "header.h"
#include "operatii_matrici.cpp"
#include "operatii_vectori.cpp"


void main()
{int choose;
	cout<<"Incepe rularea programului ce realizeaza diferite operatii cu vectori si matrici"<<endl;
	cout<<"Pentru operatii cu vectori apasati 1"<<endl;
	cout<<"Pentru operatii cu matrici apasati 2"<<endl;
	cin>>choose;
	switch(choose){
case 1:
	cout<<"Ati ales sa realizati operatii cu vectori"<<endl;
	VECTOR v;
	puts("Introduceti dimensiunea vectorului v:");
	cin>>v.n ;

	 cit_vect(v.v,v.n);//citeste  vectorul v de la tst

	 cout<<endl<<endl<<"Vectorul introdus este:";
	 print_vect(v.v,v.n,"v");//afiseaza vectorul citit

	 sum_prod_vect(v.v,v.n);
     max_min_vect(v.v,v.n);
	 ord_cresc_descresc(v.v,v.n,"v");
	

	cout<<endl;
	puts("Apasati o tasta  a continua cu operatii specifice pentru 2 vectori");
	getch();

	VECTOR x,y;
	MATRICE c,l;//declararea este necesara pentru realizarea vectorului linie si a vect coloana
	puts("Introduceti dimensiunea vectorului X:");
	cin>>x.n ;
	cit_vect(x.v,x.n);//citeste  vectorul x de la tst
	puts("Introduceti dimensiunea vectorului Y:");
	cin>>y.n ;
	cit_vect(y.v,y.n);//citeste  vectorul y de la tst

	if(suma_vectori(x.v,y.v,x.n,y.n)==0)puts("Operatia de adunare a celor doi vectori este imposibila");
	if(prod_vectori(x.v,y.v,x.n,y.n)==0)puts("Operatia de inmultire a celor doi vectori este imposibila");
	if(prod_scalar(x.v,y.v,x.n,y.n)==0)puts("Produsul scalar nu poate fi realizat-cei doi vectori au dimensiuni diferite");

	puts("Introduceti dimensiunea vectorului coloana:");
	cin>>c.lin;
	c.col=0;
	puts("Introduceti dimensiunea vectorului linie:");
	cin>>l.col;
	l.lin=0;
	int i;
	cout<<"Introduceti cele "<<c.lin<<" elemente ale vectorului coloana"<<endl; 
	for(i=0;i<c.lin;i++)cin>>c.a[i][0];
	cout<<"Introduceti cele "<<l.col<<" elemente ale vectorului linie"<<endl; 
	for(i=0;i<l.col;i++)cin>>l.a[0][i];

	if(prodv_col_lin(c.a,l.a,c.lin,l.col)==0)puts("Produsul dintre vectorul linie si vectorul coloana este imposibila");
break;

case 2:
puts("Ati ales  operatii specifice unei matrici");
	
	MATRICE mat;
	puts("Introduceti nr de linii al matricii:");
	cin>>mat.lin;
	puts("Introduceti nr de coloane al matricii:");
	cin>>mat.col;
     cit_mat(mat.a,mat.lin,mat.col);
	 print_mat(mat.a,mat.lin,mat.col,"mat");
	 suma_prod_mat(mat.a,mat.lin,mat.col);
	 max_min_mat(mat.a,mat.lin,mat.col);
	 el_poz_neg_nul(mat.a,mat.lin,mat.col);
	 suma_el_diag(mat.a,mat.lin,mat.col);

	 cout<<endl;
	 puts("Apasati o tasta  a continua cu operatii specifice pentru 2 matrici");
	 getch();

	MATRICE A,B;
	puts("Introduceti nr de linii al matricii A:");
	cin>>A.lin;
	puts("Introduceti nr de coloane al matricii A:");
	cin>>A.col;
	cit_mat(A.a,A.lin,A.col);
	puts("Introduceti nr de linii al matricii B:");
	cin>>B.lin;
	puts("Introduceti nr de coloane al matricii B:");
	cin>>B.col;
	cit_mat(B.a,B.lin,B.col);
    
	if(suma_mat(A.a,B.a,A.lin,A.col,B.lin,B.col)==0)cout<<"Suma matricilor nu este posibila.Matricile nu au aceeasi dimensiune"<<endl;
	if(prod_mat(A.a,B.a,A.lin,A.col,B.lin,B.col)==0)cout<<"Produsul matricilor este imposibil"<<endl;
	
	getch();

default:
cout<<"Alegere inexistenta"<<endl;
getch(); 
		
	break;
}
}


The function used have been verified and are working well.
Last edited on
You should save your code in text file and then make a new project.
still not working:( . same error
Because you're doing the same wrong thing. In you code you have function definitions. Not the prototype - the actual code that does the work.

In ALL your cpp files in any one program, you must have this code once and only once. It's NOTHING to do with header guards.

When you #include someFile , the entire contents of someFile is copied completely into that spot, so if someFile contains a function definition, you've now got the same function definition in more than one place. This is BAD.
I have defined the prototype in the header file.I have defined the function used in main ONLY ONCE in 2 different .cpp files(different functions in different files)).I have included them as seen in the main.cpp script.
I do not see where i have defined the functions TWICE.
I have defined the function used in main ONLY ONCE in 2 different .cpp files


So, once in one cpp file, and once (again) in another cpp file? That's more than once. Then, when you include them both in your main.cpp file, you've defined them again!

If your cpp files contain function definitions, you are misusing them by #includeing them. Conventionally, *.h files are #included, and *.cpp files are compiled separately and then linked by the linker. This convention exists for a very good reason.
Last edited on
What Moschops is saying is this.

You have defined your function in "operatii_matrici.cpp" and "peratii_vectori.cpp". That is one definition, now you #include those files in main, which brings in the contents of those files into main, now you have 2 definitions of each. One in the translation unit for main and one in each of the 2 other .cpp files.

How to fix this?
In main:
#include "header.h"

In operatii_matrici.cpp
#include "header.h"

In operatii_vectori.cpp
#include "header.h"

Including the header with just the declarations (for the purpose of this thread) is like declaring a function that you define in the main.cpp file, Below the definition of your main function, at the top of your main.cpp file.
It worked.
tnx Moschops for identifying the problem and tnx clanmjc for the beginner explanation.
It seems the problem was an easy one,but what can i say beginner thinking :)).
tnx again guys and a good day
Topic archived. No new replies allowed.