0

Following code compiles without error or warning. But obviously I put an extra Person in the implementation part. How come this compiles? Actually I also tried to put more Person but it seems the code almost compiles. But if use one::one::Person::set_name, gcc will emit error msg.

nm -C person.o result:

0000000000000000 T one::Person::set_name(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

It seems compile can "fold" class scope, which make multiple class Person scope into a single class scope

Centos 8 gcc version 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)

// header file
#include <string>

namespace one {
  class Person {
    public:
      void set_name(const std::string& name);
      void set_age(int age);

    private:
      std::string name_;
      int age_;
  };
}

// implementation
#include "person.h"

// extra Person in the following line.
void one::Person::Person::set_name(const std::string& name) {
  name_ = name;
}
Xiaoyong Guo
  • 361
  • 1
  • 7
  • this question is answered, should I keep this question? – Xiaoyong Guo Jun 29 '22 at 02:16
  • 3
    I don't think so. Normally duplicate linkage helps people follow the path from one search hit and encounter the more established Q&A of the duplicates. But in your case your question is unlikely to be a search hit for anyone with a similar problem because you don't even show the compiler error. Everything in your question is specific to your class and I don't see how anyone searching on similar issues will land on this question in future. – paddy Jun 29 '22 at 02:23

0 Answers0