I have two source files, one named main.cpp (where the namespace M is defined) and the file engines.h (where several names are defined).
main.cpp includes engines.h.
engines.h need to use the stuff inside M, and M needs to use the stuff inside engines.h.
I get an error doing using namespace M; in engines.h.
You cannot do
using namespace Mbefore the namespace was defined. If there is a cyclic dependency, you need to solve it by using one or more techniquesForward declare if your uses don’t need to know the members or size of classes, but just handle with pointers or references to them:
Define stuff in engines.cc
Use of the pimpl idiom reduces dependencies even more, as it keeps headers free of headers that are only used by implementations..
Split the part in .h files for the interface and .cpp files for the implementation to handle such dependencies. That way, headers are less dependent on other headers, and implementation files can include the headers.