4
#include <iostream>
#include <algorithm>

struct Node
{
    int value_;
    Node* next_;

    Node(int value, Node* next = nullptr)
        : value_(value)
        , next_(next)
    {}
};

Node* operator++(Node* node)
{
    node = node->next_;
    return node;
}

int operator*(Node* node)
{
    return node->value_;
}

int main()
{
    Node* first = new Node(10);
    first->next_ = new Node(20);
    first->next_->next_ = new Node(17);

    Node* endIter = nullptr;

    std::cout << std::accumulate(first, endIter, 0) << std::endl;
}

In this example I have tried to use Node* as iterator for list. I am getting compiler errors

  1 main.cpp:15:28: error: Node* operator++(Node*) must have an argument of class or enumerated type
  2  Node* operator++(Node* node)
  3                             ^
  4 main.cpp:21:25: error: int operator*(Node*) must have an argument of class or enumerated type
  5  int operator*(Node* node)

Looks like I can't overload operator++ and operator* for pointers.

I have copied this overloads from the book Stroustrup: The C++ Programming Language (4th Edition) pg 703.

Can anyone explain what I have done wrong?

2 Answers 2

3

The input to std::accumulate must meet the requirements of an InputIterator.

One of the requirements of an InputIterator is that it support the pre-increment operator.

You can use the pre-increment operator on a Node* but it will use the built-in logic to increment the pointer.

Node* operator++(Node* node) { ... }

is invalid since type of the argument is Node*. You can overload operator++ for Node but not Node*.

From the C++11 Standard (emphasis mine):

13.5 Overloaded operators

6 An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

Sign up to request clarification or add additional context in comments.

7 Comments

But I have overloaded pre-increment for Node*
Ok, Can you explain how Mr. Stroustrup has done it? Here is a link to the book. dropbox.com/s/ipo5pkud6j4vr30/Straustrup4th.pdf?dl=0
@Ashot, the book uses double ad[] = {1,2,3,4}; double s1 = accumulate(ad,ad+4,0.0); double s2 = accumulate(ad,ad+4,0);. Those are perfectly valid iterators.
At the top of the page is written Node∗ operator++(Node∗ p) { return p−>next; }. It is correct?
@Ashot, I see it now. I am surprised. It's most likely an error that's been corrected in a later version.
|
0

You can't overload operators for primitive type or point. So you should write a iterator for Node.

class iterator {
public:
  iterator(Node *node): _node(node) {}
  iterator operator++() {
    _node = _node->next;
    return *this;
  }
  iterator operator++(int) {
    iterator tmp = *this;
    ++(*this);
    return tmp;
  }
  bool operator == (const iterator &iter) const {
    return _node == iter._node;
  }
  int operator*() {
    return _node->value;
  }
private:
  Node *_node;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.