'pimpl'관용어로 가려고했지만 나는 컴파일 할 수 없습니다.pimpl 관용구 컴파일시 문제
g의 ++ v의 리눅스 민트에4.6.3 나는 다음과 같은 오류 얻을 :.
#ifndef PERSON_HH
#define PERSON_HH
#include <tr1/memory>
#include <string>
class Person
{
private:
class PersonImpl;
std::tr1::shared_ptr<PersonImpl> pImpl;
public:
Person(const std::string& name, int age=0);
~Person();
const std::string& get_name() const;
int get_age() const;
};
#endif
person.cc
person.hh : 이것은 내 코드
$ g++ main.cc
/tmp/ccXQ9X9O.o: In function `main':
main.cc:(.text+0xd7): undefined reference to `Person::Person(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
collect2: ld returned 1 exit status
입니다
#include <string>
#include "person.hh"
class Person::PersonImpl
{
public:
std::string name;
int age;
PersonImpl(const std::string& n, int a) : name(n), age(a) {}
};
Person::Person(const std::string& name, int age) : pImpl(new PersonImpl(name, age)) {}
Person::~Person() {}
const std::string& Person::get_name() const { return pImpl->name; }
int Person::get_age() const { return pImpl->age; }
main.cc
#include <iostream>
#include "person.hh"
int main()
{
const std::string name = "foo";
Person p(name, 50);
return 0;
}
코드 실수는 별도로 'pimpl'관용어를 모방 한 방법에 대해 조언 해 주실 수 있습니까? 이것에 부합합니까?
unique_ptr 제안과이 특정 사례에 대한 초기화 프로그램 목록을 보내 주셔서 감사합니다. 링크 문제를 해결하면 코드에 몇 가지 다른 문제가 있음을 발견했습니다. – Nobilis
@Nobilis : 다행스럽게도 도움이되었습니다. :) –