hi, i was writing some basic programs and linking them together in 1 big program but when i decided that i want to have some functions posible in many files(programs) i run in error i couldn't fix about multiple definitions
Hers little bit butchered source code after my atempts to fix it
Don't laught to hard ;)
header.h
#include <iostream>
#include "header.h"
usingnamespace std;
int main () {
constint n_max=3; //max izveles varianti
int n;
do {
cout <<"\n\n============================================\n";
cout <<"0. Lai izietu no programas\n";
cout <<"1. Noapalot kvadratiskas matricas nediagonalu elementu vertibas uz tuvakajiem veselajiem skaitliem\n";
cout <<"2. Noteikt matricas vienados elementu un to indeksus\n";
cout <<"3. Noteikt matricu elementu un ta indeksu, kuram ir minimala vertiba rindina un maksimala vertibastabina\n";
cout <<"\nKuru no dotajam programam velaties palaist? ";
do {
cin >>n;
if (n>n_max) { cout <<"Tads programas nummurs neeksiste, ievadiet citu nummuru\n";}
} while (n>n_max);
cout <<"\n============================================\n\n";
if (n==1) { Prog_01 (); }
if (n==3) { Prog_03 ();}
} while (n!=0);
return 0;
}
#include <iostream>
#include <cstdlib> // rand()
#include "header.h"
usingnamespace std;
constint max_=8;
void Prog_01 () {
double mas [max_][max_];
int x;
sakums(1);
//cout <<"Programas 1 sakums\n\n";
for ( int i=0;i<max_;i++) { //
for (int j=0;j<max_;j++) { //uztaisa
x=rand()%1000; //masivu ar
if (x<100) {x=x+100;} //random double
if (x%10==0) {x=x+rand()%10;} //skaitliem
mas[i][j]=x*0.1; //
}
}
for ( int i=0;i<max_;i++) { //
for (int j=0;j<max_;j++) { //izdruka masivu
std::cout<<mas[i][j]<<" "; //uz ekrana
} //
cout<<endl; //
}
for (int i=0;i<max_;i++) { //
for (int j=0;j<max_;j++) { //Noapalo masivu
if (i!=j && i+j!=7) { //abas
x=int(mas[i][j]+0.5); //diognales
mas[i][j]=x; //atstajot
} //
}
}
cout <<endl;
for ( int i=0;i<max_;i++) { //
for (int j=0;j<max_;j++) { //Izdruka masivu uz ekrana
std::cout<<mas[i][j]<<" "; //
} //
cout<<endl; //
}
//cout <<"\nProgramas 1 beigas";
//beigas(1);
}
Error messege
1 2 3 4 5 6 7 8 9 10 11 12
Compiling: Global.cpp
Compiling: Main.cpp
Compiling: Prog_01.cpp
Compiling: Prog_03.cpp
Linking console executable: bin/Release/PD_2
obj/Release/Prog_01.o: In function `sakums(int)':
Prog_01.cpp:(.text+0x30): multiple definition of `sakums(int)'
obj/Release/Main.o:Main.cpp:(.text+0x30): first defined here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
0 errors, 0 warnings
header.h declares function sakums(int x), so each .cpp file that includes it provides an copy. At link time, the linker sees all three copies and doesn't know which one you want to use.
To fix, define sakums(int x) in a .cpp file and leave a prototype in the header.
You could, of course, make sakums(int x) in inline function in header.h, but you shouldn't do that unless you have good reason to.
thx kbw it worked but now i have another question.
Why when i tried this http://en.wikipedia.org/wiki/Include_guard
in header.h file it didn't work since from wiki i understood it forces linker to make only 1 copy of file.