2014-07-17 3 views
0

shared_ptr 데이터 멤버가있는 클래스가 있습니다. 아래는 내가 메인 프로그램에 포함 된 단지 헤더 내 프로그램을 컴파일하려고 예를 들어shared_ptr with map (오류 오류 C2664)

class A { 
private: 
    shared_ptr<map<int, std::string>> _pMap; 
    A(); 
public: 
    A(shared_ptr<map<int, std::string>>); 
    A(const A& source); 
    A& operator=A(const A&); 
}; 

A::A(shared_ptr<map<int, std::string>> mapPtr) 
: _pMap(new std::shared_ptr<std::map<int, std::string>>()) { 
    _pMap = mapPtr; 

A::A(const A& source) : _pMap(source._p) {} 

A& A::operator=(const A& source) { 
    if (this == &source) { 
     return *this; 
    } 

    _pMap = source._pMap; 

    return *this; 
} 

이다, 나는 다음과 같은 오류가 나타납니다

error C2664: 'std::_Ptr_base<_Ty>::_Reset0' : 
      cannot convert parameter 1 from 'std::shared_ptr<_Ty> *' 
      to 'std::map<_Kty,_Ty> * 

을하지만 내가 뭐하는 거지 어디 확실하지 않다 이. 이것이 일어날 수있는 이유에 대해 누군가 안내해 주시겠습니까?

감사합니다.

+0

'그러나 나는 어디에서이 일을하고 있는지 모르겠다.'그리고 당신은 오류를 복제하는 * 완전한 * 그러나 최소한의 예제를 게시하지 않습니다. 그러나 왜 사용자 정의 할당 연산자와 복사 ctor를 작성하고 있습니까? 기본 클래스는 클래스에 완벽하게 적합합니다. – PaulMcKenzie

+0

나는 문제가'A (const A & source);와'A & operator = (const A & source)'일 수 있다고 생각한다. 이동 생성자와 할당이 필요합니다. –

답변

4

문제 (또는 중 적어도 하나는)

A::A(shared_ptr<map<int, std::string>> mapPtr) : _pMap(new std::shared_ptr<std::map<int, std::string>>()) { 
     _pMap = mapPtr; 

그것이

A::A(shared_ptr<map<int, std::string>> mapPtr) : _pMap(new std::map<int, std::string>()) { 
     _pMap = mapPtr; 

해야하지만 두 번 _pMap을 초기화하기가 지적되지 않는 선에서입니다 - 그래서이 생성자 최선 내가 코드의 일부를 고정

A::A(shared_ptr<map<int, std::string>> mapPtr) : _pMap(mapPtr) { } 
+0

어쨌든 초기화에 아무런 포인트가 없다는 것에 유의하십시오. 그것은'_pMap (mapPtr)'이어야합니다. – juanchopanza

+0

오 예이 코드는 이상합니다 ... –

+0

Ahh. 내가 참조. 감사. 그건 의미가 있습니다. –

0

할 것 :

,
class A 
{ 
private: 
    shared_ptr<map<int, std::string>> _pMap; 
    A(); 
public: 
    A(shared_ptr<map<int, std::string>>); 
    A(const A& source); 
}; 

A::A(shared_ptr<map<int, std::string>> mapPtr) 
{ 
    _pMap = mapPtr; 
} 

int main() 
{ 
    shared_ptr<map<int, std::string>> myMap = std::make_shared<map<int, std::string>> 
    (); 
    A a(myMap); 
    return 0; 
}