[solved]multiple definition of `sakums(int)'

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef H_HEADER
#define H_HEADER
void Prog_01 ();

void Prog_03 ();

void sakums (int);
//void beigas (int);

void sakums (int x)
{
    //std::cout <<"\n\n"<<x<<". Programas sakums\n\n";
    std::cout <<"aaaaa";
}

//void beigas (int x)
//{
//    std::cout <<"\n\n"<<x<<". Programas beigas\n\n";
//}
#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
#include <iostream>
#include "header.h"
using namespace std;
int main () {
    const int 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;
}

Prog_01.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
#include <iostream>
#include <cstdlib> // rand()
#include "header.h"

using namespace std;
const int 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
Last edited on
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.
Include guards work for multiple declaration, your problem was having multiple implementations
Topic archived. No new replies allowed.