sftrabbit / CppPatterns-Patterns

A repository of modern C++ patterns curated by the community.
Creative Commons Zero v1.0 Universal
1.43k stars 223 forks source link

pimpl.cpp not compiled on macOS 10.13.3 #69

Closed impvd closed 6 years ago

impvd commented 6 years ago

My code as follows:

/*
~/D/t/c/p/smart $ ls
foo.cpp foo.h   main.cc
~/D/t/c/p/smart $ cat foo.cpp
*/
#include "foo.h"
#include <memory>

class foo::impl
{
  public:
    void do_internal_work()
    {
      internal_data = 5;
    }
  private:
    int internal_data = 0;
};
foo::foo()
  : pimpl{std::make_unique<impl>()}
{
  pimpl->do_internal_work();
}
foo::~foo() = default;
foo::foo(foo&&) = default;
foo& foo::operator=(foo&&) = default;

//~/D/t/c/p/smart $ cat foo.h
#include <memory>
class foo
{
  public:
    foo();
    ~foo();
    foo(foo&&);
    foo& operator=(foo&&);
  private:
    class impl;
    std::unique_ptr<impl> pimpl;
};

//~/D/t/c/p/smart $ cat main.cc
#include "foo.h"

#include <iostream>

int main() {
    foo x;
}

Compile result:

~/D/t/c/p/smart $ clang++ -std=c++14 main.cc -o main
Undefined symbols for architecture x86_64:
  "foo::foo()", referenced from:
      _main in main-b39a70.o
  "foo::~foo()", referenced from:
      _main in main-b39a70.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)