Error Definition Of Is Not In Namespace Enclosing
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up gcc failes to compile operator definition with prefixed namespace up vote 7 down vote favorite I have operators declared for class my_type in namespace my_namespace. namespace my_namespace { class my_type { friend std::ostream& operator << (std::ostream& out, my_type t); } } I'm trying to define these operators in implementation file, but when I write something like that std::ostream& my_namespace::operator << (std::ostream& out, my_type t) { out << t; return out; } I get error message error: ... operator should have been declared inside 'my_namespace' When I change it to namespace my_namespace { std::ostream& operator << (std::ostream& out, my_type t) { out << t; return out; } } then it's compiles, but I don't understand the problem. Why does this failed to compile? Is there everything right with that? I would appreciate link to standard as I really don't know what to search. added file.h #ifndef A_H #define A_H #include
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Compile error when defining a member function, but only in GCC up vote 11 down http://stackoverflow.com/questions/4757871/gcc-failes-to-compile-operator-definition-with-prefixed-namespace vote favorite 1 The following program compiles without errors with MSVS, clang and GCC: class A; namespace Y { using ::A; class A {}; } int main() {} Now let's define a member function. Now it still compiles with MSVS and clang, but not with GCC: class A; namespace Y { using ::A; class A { void f() {} }; } int main() {} GCC gives the following error message: prog.cc:5:22: http://stackoverflow.com/questions/31232186/compile-error-when-defining-a-member-function-but-only-in-gcc error: definition of 'void A::f()' is not in namespace enclosing 'A' [-fpermissive] Why is that? Is this a bug in GCC? If the second version of the program violates a rule of the c++ standard, what rule does it violate and why doesn't MSVS and clang give a diagnostic message for that violation? Is this a case of an ambiguity of the c++ standard? From the error message it looks like GCC incorrectly thinks we have a violation of the following rule: http://eel.is/c++draft/class.mfct#2 "A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition." We do not have a violation of this rule since the member function definition is inside the class definition. My theory is that GCC confuses the declaration class A; in the global namespace with the class definition class A { ... } in the namespace Y. I think we have a bug in GCC. With GCC they declare the same entity. This can be seen by observing that in the first version of the program it possible to use ::A as a complete type in main when compiling with GCC. Same for MSVS. With Clang however they declare different entities. This difference may be because of
Login: [x] User account creation filtered due to spam. Bug66879 - Error message when defining a member https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66879 function inside a class definition Summary: Error message when defining a member function inside a class definition Status: NEW Alias: None Product: gcc Classification: Unclassified Component: c++ (show other bugs) Version: 6.0 Importance: P3 normal Target Milestone: --- Assignee: Not yet assigned to anyone URL: Keywords: accepts-invalid Duplicates (4): 66878 66888 66889 66901 (view as bug list) Depends on: error definition Blocks: Reported: 2015-07-15 14:03 UTC by Anders Granlund Modified: 2015-07-27 01:09 UTC (History) CC List: 1 user (show) webrown.cpp See Also: Host: Target: Build: Known to work: Known to fail: Last reconfirmed: 2015-07-15 00:00:00 Attachments prog.cc used in the command line (85 bytes, text/plain) 2015-07-15 14:03 UTC, Anders Granlund Details View All Add an attachment (proposed patch, testcase, etc.) error definition of Note You need to log in before you can comment on or make changes to this bug. Description Anders Granlund 2015-07-15 14:03:35 UTC Created attachment 35988 [details] prog.cc used in the command line Compile the attached program with the following command line: gcc prog.cc The output is the following: prog.cc:6:22: error: definition of ‘void A::f()’ is not in namespace enclosing ‘A’ [-fpermissive] class A { void f() {} }; ^ The expected behaviour is not to get this error message. The error message seem to suggest that the compiler thinks that the program violates [class.mfct]/2 where one sentence says: "A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition." Observe however that the member function definition in the given program is made inside the class definition. Note that if we remove the member function definition in the given program it compiles without errors. Output of gcc -v: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-sha