circular referencing Linker error 2005

Hi all,

Can you please suggest how I can avoid such a linker error without moving my function some where else?

So I have 3 header files: a.hpp, b.hpp, and c.hpp. a #include b, b #includes c, c include #a. All of the header files have header guard. There is a function in b.hpp, called addition which takes 2 integers and return the sum of them. When I call the addition() in my main, it doesn't compile and throws a linker error. I would think header guard on each file would avoid the error but it just doesn't.


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
 // a.hpp

#ifndef  A_HPP
#define A_HPP

#include "b.hpp"

#endif // ! A_HPP


// b.hpp

#ifndef  B_HPP
#define B_HPP

#include <iostream>
#include "c.hpp"

int addition(int x ,int y) {return x + y;}

#endif // ! B_HPP


// c.hpp

#ifndef  C_HPP
#define C_HPP

#include "a.hpp"

#endif // ! A_HPP

// source file

#include "a.hpp"
#include <iostream>

int main() {

    std::cout << addition(10, 10);
}
Why would you make this?
No idea, works for me.
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
$ for i in *.hpp *.cpp ; do  echo ===== $i ===== ; cat $i ; echo; done
===== a.hpp =====
#ifndef  A_HPP
#define A_HPP

#include "b.hpp"

#endif // ! A_HPP

===== b.hpp =====
#ifndef  B_HPP
#define B_HPP

#include <iostream>
#include "c.hpp"

int addition(int x ,int y) {return x + y;}

#endif // ! B_HPP

===== c.hpp =====
#ifndef  C_HPP
#define C_HPP

#include "a.hpp"

#endif // ! A_HPP
===== d.cpp =====
#include "a.hpp"
#include <iostream>

int main() {

    std::cout << addition(10, 10);
}
$ g++ d.cpp
$ ./a.out 
20$ 


And without iostream ballooning the output, the include guards do what they're supposed to.
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
$ g++ -E d.cpp 
# 1 "d.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "d.cpp"
# 1 "a.hpp" 1



# 1 "b.hpp" 1




# 1 "c.hpp" 1



# 1 "a.hpp" 1
# 5 "c.hpp" 2
# 6 "b.hpp" 2

int addition(int x ,int y) {return x + y;}
# 5 "a.hpp" 2
# 2 "d.cpp" 2


int main() {

    std::cout << addition(10, 10);
}


//highwayman

It's from my school project. We were given a solution that already has codes in, and we need to extend it to complete some tasks. Some header files directly or indirectly include each other.


//salem c

Thank you for testing this. I run my codes on visual studio and it doesn't compile.
Last edited on
Just out of curiosity, could you please tell us if Visual Studio accepts this structure?

a.hpp:
1
2
3
4
5
6
7
8
#ifndef A_HPP
#define A_HPP


#include "b.hpp"


#endif // ! A_HPP 


b.hpp:
1
2
3
4
5
6
7
8
9
10
11
#ifndef B_HPP
#define B_HPP


#include "c.hpp"


int addition(int x, int y);


#endif // ! B_HPP 


b.cpp:
1
2
3
4
5
6
7
#include "b.hpp"


int addition(int x, int y)
{
    return x + y;
}


c.hpp
1
2
3
4
5
6
7
8
#ifndef C_HPP
#define C_HPP


#include "a.hpp"


#endif // ! C_HPP 


main.cpp
1
2
3
4
5
6
7
#include "a.hpp"
#include <iostream>

int main() {

    std::cout << addition(10, 10);
}

// Enoizat

It worked! Could you please explain why?
I don't have Visual Studio, so I don't know exactly why :-) but it's a good habit to put the function prototypes into the headers and the definitions into the 'source' files.
Maybe Visual Studio is set to consider it more an obligation than a habit (just an hypothesis).
Topic archived. No new replies allowed.