문제

Okay, so I have been searching every forum on this "multiple definition first defined here" problem for the last 4 hours, and I guess I must be really stupid, but I can't seem to solve this problem.

I have the following files:

main.cpp
Socket.h
Socket.cpp
Server.h
Server.cpp
Packet.h
FileHandlerIn.h
FileHandlerOut.cpp

main.cpp includes only Server.h, which declare some functions, which are then defined in Server.cpp.
Server.cpp uses functions from Socket.cpp, FileHandlerOut.cpp and Packet.h, so the header files Socket.h, Packet.h and FileHandlerIn.h are included in Server.h. None of the other header files includes anything else than standard libraries.

What happens is that every function in Server.cpp and every function in Packet.h gets a multiple definition error. The reason I have functions inside Packet.h is that I have a struct defined, and the Packet.h functions all operate on the struct. I'm really confused about how I should correctly define these functions, so I can use them in other cpp files, and this might be part of my problem?

도움이 되었습니까?

해결책

every function in Packet.h gets a multiple definition error

If you're going to define functions in a header (outside a class definition), you'll need to mark them inline. This relaxes the One Definition Rule to allow definitions in multiple translation units.

You're still only allowed one definition in each translation unit, so make sure the headers have include guards.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top