C++ set 컨테이너를 사용하려고 하는데 원인불명의 에러가 납니다.

greathero의 이미지

#include <iostream>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
 
class President
{
public:
     int Id;
     string Name;
     string Addr;
 
     President(int aId,char *aName, char *aAddr)
          : Id(aId), Name(aName), Addr(aAddr) { }
     void OutPresident() {
          printf("Id:%d, 이름:%s, 주소:%s\n",Id,Name.c_str(),Addr.c_str());
     }
     bool operator<(const President &Other) const {
          return Id < Other.Id;
     }
     bool operator==(const President &Other) {
          return (Id==Other.Id && Name==Other.Name && Addr==Other.Addr);
     }
};
 
void main()
{
     set<President> King;
     King.insert(President(516,"박정희","동작동"));
     King.insert(President(1212,"전두환","연희동"));
     King.insert(President(629,"노태우","강북"));
     King.insert(President(3030,"김영삼","상도동"));
     King.insert(President(1234,"김대중","강남"));
 
     set<President>::iterator it;
     for (it=King.begin();it!=King.end();it++) {
          (*it).OutPresident();
     }
     President ZeroThree(3030,"아무개","아무데나");
     it=King.find(ZeroThree);
     if (it != King.end()) {
          cout << "검색되었음" << endl;
          (*it).OutPresident();
     }
     it=find(King.begin(),King.end(),ZeroThree);
     if (it != King.end()) {
          cout << "검색되었음" << endl;
          (*it).OutPresident();
     }
}

소스 출처: http://www.winapi.co.kr/clec/cpp4/41-1-4.htm

위와 같은 소스를 Visual Studio 2010에서 실행해보려고 했는데
"개체에 멤버 함수와 호환되지 않는 형식 한정자가 있습니다."라는 에러가 뜨네요ㅠ

이게 무슨 말인지 이해도 잘 안되고... 근데 아무리 봐도 딱히 에러가 있을만한 곳은 없는거 같은데...
왜 에러가 날까요?

greathero의 이미지

(*it).OutPresident(); <- 이 코드에서 뜹니다. 이 문장이 코드 내에 3개가 있어서 3개의 에러가 떠요.

qiiiiiiiip의 이미지

greathero의 이미지

it->mem_fun();의 방식으로 호출이 되었던 것을

const_cast<obj_type&>(*it).mem_fun(); 으로 바꿔주면 됩니다.

*it의 상수 성질을 날려버리면 mem_fun이 const가 아니어도 호출을 할 수 있습니다.

그런데, 왜 이렇게 하면 const를 쓰지 않고도 할 수 있는지 좀 궁금합니다.
set에서는 객체가 const T가 아닌 T타입으로 들어가는데 말이죠.

kukyakya의 이미지

set에는 객체가 const T로 들어가게 됩니다. 이는 set을 'key만 존재하는 map'으로 생각하시면 편한데, associative container는 key를 기준으로 정렬하게 되므로 key가 변화하지 않아야 정렬상태를 유지할 수 있어 key type에 const가 필요합니다.

구현에 따라 다를 수 있지만 일반적으로 map의 경우 pair 타입을 저장하고, set의 경우 const K 타입을 저장합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.