In Visual C++, i faced “fatal error LNK1169: one or more multiply defined symbols found” with below code, How can i solve the problem if i wanna include both header file in both source for the other functions usage?
main.cpp
========
#include main.h
#include sub.h
sub.cpp
========
#include main.h
#include sub.h
sub.h
=========
typedef struct{
char colour;
char name;
}person;
person ssss = { red, ali};
Your problem is you are defining a variable in the header file:
One is instantiated in main.cpp and one in sub.cpp. You would be better to put an:
In the header file and then define the variable once in one of the source files. This will let both source files know it exists and both will refer to the same variable, assuming this is what you want. Like the other answers have suggested I would definitely suggest using header guards too, whilst you don’t specifically need them for this example it is good practice and will save you headaches in the long run.