I am trying to understand how to work with header file in C++. Faced an error and I need know why and how to solve this.
I have 4 files.
main.cpp
B.h
A.h
A.cpp
Among them in some file. for ex. main.cpp or B.h if the line #include "B.h"
exists, it gives me error.
main.cpp:
#include <iostream>
#include "A.h"
#include "B.h"
int main()
{
B obj_00;
A obj_01(obj_00);
return 0;
}
B.h:
#include <iostream>
#ifndef _B_H_
#define _B_H_
class B
{
private:
public:
B();
~B();
};
B::B(){//some code};
B::~B(){//some code};
#endif //_B_H_
A.h:
#ifndef _A_H_
#define _A_H_
class A
{
private:
public:
A();
A(class B &temp);
A(class A &temp);
~A();
};
#endif //_A_H_
A.cpp
#include <iostream>
#include "A.h"
#include "B.h"
A::A(A &temp){//some code};
A::A(B &temp){//some code};
A::~A(){//some code};
Error:
C:\Users\jamil\AppData\Local\Temp\ccNRBm6n.o: In function `B::B()':
E:/Personal/Programming/VSCode/test/B.h:13: multiple definition of `B::B()'
C:\Users\jamil\AppData\Local\Temp\ccsOpoic.o:E:/Personal/Programming/VSCode/test/B.h:13: first defined here
C:\Users\jamil\AppData\Local\Temp\ccNRBm6n.o: In function `B::B()':
E:/Personal/Programming/VSCode/test/B.h:13: multiple definition of `B::B()'
C:\Users\jamil\AppData\Local\Temp\ccsOpoic.o:E:/Personal/Programming/VSCode/test/B.h:13: first defined here
C:\Users\jamil\AppData\Local\Temp\ccNRBm6n.o: In function `B::~B()':
E:/Personal/Programming/VSCode/test/B.h:18: multiple definition of `B::~B()'
C:\Users\jamil\AppData\Local\Temp\ccsOpoic.o:E:/Personal/Programming/VSCode/test/B.h:18: first defined here
C:\Users\jamil\AppData\Local\Temp\ccNRBm6n.o: In function `B::~B()':
E:/Personal/Programming/VSCode/test/B.h:18: multiple definition of `B::~B()'
C:\Users\jamil\AppData\Local\Temp\ccsOpoic.o:E:/Personal/Programming/VSCode/test/B.h:18: first defined here
collect2.exe: error: ld returned 1 exit status