I have two modules that I have separated out into two different .h
and .cpp
files. I would like to have both modules be declared in the same namespace, but I receive the error candidate found by name lookup is 'my_namespace'
and error: reference to 'my_namespace' is ambiguous
. Why is this the case?
I have listed below my cmake config, the two header files and the module_b source file.
file(
GLOB HEADER_LIST CONFIGURE_DEPENDS
"${myproj_SOURCE_DIR}/include/myproj/*.h"
)
# Make an automatic library - will be static or dynamic based on user setting
add_library(
${CMAKE_PROJECT_NAME}
module_a.cpp
module_b.cpp
${HEADER_LIST}
)
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ../include)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ./)
# All users of this library will need at least C++17
target_compile_features(${CMAKE_PROJECT_NAME} PUBLIC cxx_std_17)
// module_a.h
namespace my_namespace { // << note: candidate found by name lookup is 'my_namespace'
struct My_Struct_A {
int a;
};
}
// module_b.h
#include <my_proj/module_a.h>
namespace my_namespace { // << note: candidate found by name lookup is 'my_namespace'
struct My_Struct_B {
int b;
My_Struct_A ab;
};
My_Struct_B do_something();
}
// module_b.cpp
#include <my_proj/module_b.h>
my_namespace::My_Struct_B my_namespace::do_something() { // << error: reference to 'my_namespace' is ambiguous
return My_Struct_B { 1, { 2 } };
}