-2

I have a class that is like

class HtmlRover
{
public:
    HtmlRover(const std::string::const_iterator &, const std::string::const_iterator &);
    ~HtmlRover();
    // ...
private:
    /...
    std::string::const_iterator _curIter;
    const std::string::const_iterator _offend;
    /...
};

and my compiler is complaining about my instantiation of _offend in the implementation of the constructor:

HtmlRover::HtmlRover(const std::string::const_iterator & it1, const std::string::const_iterator & it2)
{
    _curIter = it1;
    _offend = it2; 
}

It says

No vaiable overload "="

Now I understand this probably has something to do with a const variable being set to a reference of a variable, but then again the reference is also const, so what's the problem? I know that const variables cannot exist without being initialized, but that also should not be a problem since _offend is getting initialized right when an HtmlRover object is initialized.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 3
    Instead of trying to fix the syntax issue, i'd first ask yourself why a Rover object would ever need to cache an iterator. http://en.wikipedia.org/wiki/Code_smell – tinkertime Nov 21 '14 at 16:21
  • 2
    `+1` for the question title, `-1` for the question based in misconception and bad coding style (not using initializer lists). – rubenvb Nov 21 '14 at 16:22
  • Use initialization list, as it is now `_offend` is using the assignment operator which is obviously a no go since it's const. – Ylisar Nov 21 '14 at 16:23
  • @rubenvb: I know the old title had a nice alliteration, but it's hardly PC. But I don't want an edit war with you so if you feel strongly about the original form, change it back. – Bathsheba Nov 21 '14 at 16:26
  • PC? It's an expression in the English language. It saved me from downvoting the question.. – rubenvb Nov 21 '14 at 16:27
  • 2
    @rubenvb Honest question: Why the `-1` for the question based on what sounds like programmer inexperience? If we downvote and reject questions with inexperienced code, then we've kind of defeated part of the purpose of SO. – ajp15243 Nov 21 '14 at 16:36
  • @ajp5243 It is important for a beginner to at least grasp the important constructs of a language, in this case the inability to assign to a `const` object (which is quite apparent here, unlike e.g. in a `const` member function where the `this` pointer makes everything muddy in error messages) and the constructor's initiliazer list. Every piece of decent tutorial mentions that `const` members (and reference members) are exactly what the initializer list is required for. That and the existence of an exact duplicate. But hey, as everything I put on this site, it's just my opinion `;-)` – rubenvb Nov 21 '14 at 18:54

2 Answers2

10

As _offend is const you need to use a base member initialiser to set its value:

HtmlRover::HtmlRover(const std::string::const_iterator & it1, const std::string::const_iterator & it2) :
   _offend(it2)
{
    _curIter = it1;     
}

It would be a good idea to treat _curIter similarly although the language will not insist on that.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
6

You have to initialize const members in an initializer list:

HtmlRover::HtmlRover(const std::string::const_iterator & it1, 
                     const std::string::const_iterator & it2)
: _curIter(it1) // can be here
, _offend(it2) // must be here
{
}
Barry
  • 286,269
  • 29
  • 621
  • 977