Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

C++ 함수를 const로 바꾼 경우 C2440 컴파일 오류가 발생한다면?

예를 들어 다음의 소스 코드에서,

#include "stdafx.h"
#include <map>

using namespace std;

class My
{
public:
    auto test() -> int;

private:
    map<int, int> _map;
};

auto My::test() -> int
{
    map<int, int>::iterator it = _map.find(5);
    return 5;
}

int main()
{
    return 0;
}

test 함수를 const 메서드로 바꾸면,

class My
{
public:
    auto test() const -> int;

private:
    map<int, int> _map;
};

auto My::test() const -> int
{
    map<int, int>::iterator it = _map.find(5);
    return 5;
}

다음과 같은 오류가 발생합니다.

Error C2440 'initializing': cannot convert from 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>' to 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>'

오류 코드의 복잡함에 당황하지 마시고 자세히 보면 답이 나옵니다.

cannot convert from 
    'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>' 
to 
    'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>'

똑똑한 stl은 const 문맥 내에서 수행될 const 함수를 미리 마련해 두었고, 따라서 위의 오류 메시지대로 다음과 같이 소스 코드를 바꿔주면 됩니다.

auto My::test() const -> int
{
    map<int, int>::const_iterator it = _map.find(5);

    return 5;
}




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/25/2018]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12457정성태12/15/202019121Windows: 185. C# - Windows 10/2019부터 추가된 SIO_TCP_INFO파일 다운로드1
12456정성태12/15/202020230VS.NET IDE: 156. Visual Studio - "Migrate packages.config to PackageReference"
12455정성태12/15/202019753오류 유형: 693. DLL 로딩 시 0x800704ec - This Program is Blocked by Group Policy [1]
12454정성태12/15/202020124Windows: 184. Windows - AppLocker의 "DLL Rules"를 이용해 임의 경로에 설치한 DLL의 로딩을 막는 방법 [1]
12453정성태12/14/202021237.NET Framework: 984. C# - bool / BOOL / VARIANT_BOOL에 대한 Interop [1]파일 다운로드1
12452정성태12/14/202021559Windows: 183. 설정은 가능하지만 구할 수는 없는 TcpTimedWaitDelay 값
12451정성태12/14/202018776Windows: 182. WMI Namespace를 열거하고, 그 안에 정의된 클래스를 열거하는 방법 [5]
12450정성태12/13/202020174.NET Framework: 983. C# - TIME_WAIT과 ephemeral port 재사용파일 다운로드1
12449정성태12/11/202021201.NET Framework: 982. C# - HttpClient에서의 ephemeral port 재사용 [2]파일 다운로드1
12448정성태12/11/202022875.NET Framework: 981. C# - HttpWebRequest, WebClient와 ephemeral port 재사용파일 다운로드1
12447정성태12/10/202021073.NET Framework: 980. C# - CopyFileEx API 사용 예제 코드파일 다운로드1
12446정성태12/10/202022168.NET Framework: 979. C# - CoCreateInstanceEx 사용 예제 코드파일 다운로드1
12445정성태12/8/202016827오류 유형: 692. C# Marshal.PtrToStructure - The structure must not be a value class.파일 다운로드1
12444정성태12/8/202018875.NET Framework: 978. C# - GUID 타입 전용의 UnmanagedType.LPStruct [1]파일 다운로드1
12443정성태12/8/202017920.NET Framework: 977. C# PInvoke - C++의 매개변수에 대한 마샬링을 tlbexp.exe를 이용해 확인하는 방법
12442정성태12/4/202017560오류 유형: 691. Visual Studio - Build Events에 robocopy를 사용할때 "Invalid Parameter #1" 오류가 발행하는 경우
12441정성태12/4/202016098오류 유형: 690. robocopy - ERROR : No Destination Directory Specified.
12440정성태12/4/202018385오류 유형: 689. SignTool Error: Invalid option: /as
12439정성태12/4/202021059디버깅 기술: 176. windbg - 특정 Win32 API에서 BP가 안 걸리는 경우 (2) [1]
12438정성태12/2/202018966오류 유형: 688. .Visual C++ - Error C2011 'sockaddr': 'struct' type redefinition
12437정성태12/1/202019626VS.NET IDE: 155. pfx의 암호 키 파일을 Visual Studio 없이 등록하는 방법
12436정성태12/1/202020016오류 유형: 687. .NET Core 2.2 빌드 - error MSB4018: The "RazorTagHelper" task failed unexpectedly.
12435정성태12/1/202027816Windows: 181. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (4) - ReuseUnicastPort를 이용한 포트 고갈 문제 해결 [1]파일 다운로드1
12434정성태11/30/202021561Windows: 180. C# - dynamicport 값의 범위를 알아내는 방법
12433정성태11/29/202019573Windows: 179. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (3) - SO_PORT_SCALABILITY파일 다운로드1
12432정성태11/29/202021400Windows: 178. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (2) - SO_REUSEADDR [1]파일 다운로드1
... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...