2014-02-28 2 views
0

오류의 구조체에 대한 잘못된 초기화 : 나는 너희들 미안 해요 코드오류 : C++

Assoc<keyType,valueType>& found = internalStorage.get(find(key));//returns the value of some key 

에 대한

invalid initialization of reference of type Assoc<float, std::basic_string<char> >& from expression of type const Assoc<float, std::basic_string<char> >

, 나는 그것이 재미 있지 알고,하지만 난 당황 해요.

어떤 아이디어가 문제입니까?

답변

1

internalStorage.get()은 값으로 개체를 반환하며 반환 된 임시 참조를 const에 바인딩하려고합니다. 당신이 found을 수정하지 않는 경우

  • 것은 참조합니다

    이 문제를 해결하는 가장 좋은 방법은 정확히 당신이 할 노력하고 무엇에 (그리고 internalStorage의 유형에) 따라 const (및 Does a const reference prolong the life of a temporary? 참조).

  • found (internalStorage에 저장된 사본)을 수정해야하는 경우 &을 삭제하면됩니다.
  • internalStorage에 저장된 객체를 수정해야하는 경우 코드를 리팩토링해야 할 수 있습니다.
+0

실제로. 참조를 const로 만들면 제대로 작동합니다. Visual C++는 불행히도 *이 코드를 허용합니다 :) – heinrichj