c++ 컴파일 에러 문제

kyj_kr의 이미지

using namespace std;
 
template <typename E>
class VectorCompleteTree {
private:
    vector<E> V;
public:
    typedef typename vector<E>::iterator Position;
protected:
    Position pos(int i) const
    {
        return V.begin() + i; // error detected!!
    }
    int idx(const Position& p) const
    {
        return p - V.begin();
    }
public:
    VectorCompleteTree() : V(1) { }
    int size() const
    {
        return V.size() - 1;
    }
    Position left(Position& p)
    {
        return pos(2*idx(p));
    }
    Position right(Position& p)
    {
        return pos(2*idx(p)+1);
    }
    Position parent(Position& p)
    {
        return pos(idx(p)/2);
    }
    bool hasLeft(const Position& p) const
    {
        return 2*idx(p) <= size();
    }
    bool hasRight(const Position& p) const
    {
        return 2*idx(p) + 1 <= size();
    }
    bool isRoot(const Position& p) const
    {
        return idx(p) == 1;
    }
    Position root()
    {
        return pos(1);
    }
    Position last()
    {
        return pos(size());
    }
    void addLast(const E& e)
    {
        V.push_back(e);
    }
    void removeLast()
    {
        V.pop_back();
    }
    void swap(const Position& p, const Position& q)
    {
        E temp = *p;
        *p = *q;
        *q = temp;
    }
};

작성한 코드 중 일부분입니다.
위 코드에서 pos 메소드에 const가 있으면 에러가 뜹니다. 없으면, 에러가 발생되지 않구요.

에러 내용은 error: could not convert '((const VectorCompleteTree*)this)->VectorCompleteTree::V.std::vector<_Tp, _Alloc>::begin >().__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ > >(((__gnu_cxx::__normal_iterator > >::difference_type)i))' from '__gnu_cxx::__normal_iterator > >' to 'VectorCompleteTree::Position {aka __g| 이렇습니다만, 별로 참고가 될거같진 않습니다 ㅠ.

제약에 걸리는 부분도 없는것같은데, 대체 왜 const 유무에 따라 에러가 발생하는걸까요??

익명 사용자의 이미지

1. 컴파일러 에러 메시지는 정말 중요합니다.
함부로 무시하거나 생략해서 전달하면 곤란하지요.

물론 이해할 수 있을 경우에나 가치가 있는 것이긴 합니다만.

STL과 관련된 컴파일러 메시지는 장황하고 이해하기 어렵기로 유명하기는 한데,
숙련된 C++ 프로그래머라면 당연히 읽을 줄 알아야 하는 것들입니다.

2. 문제는, pos를 const method로 만들었을 경우 반환하는 값의 타입이 Position이 아니게 된다는 점입니다.

pos는 const method이기 때문에, 이 메서드는 상수 객체 인스턴스에 대해 호출된 것으로 간주됩니다.
즉 이 메서드에서 V는 상수 벡터이고, 여기에 begin을 호출할 경우 const 버전의 begin이 호출됩니다.

https://en.cppreference.com/w/cpp/container/vector/begin

const 버전 begin의 반환 타입은 vector<E>::iterator가 아니라 vector<E>::const_iterator 랍니다.

익명 사용자의 이미지

감사합니다!

좀 더 공부해봐야겠군요!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.