#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;
}
이 예제에서는 목록의 반복자로 Node*
을 사용하려고 시도했습니다. 나는 컴파일러 오류를 얻고있다목록의 반복자로 'Node *'사용하기
operator++
및
operator*
를 오버로드 할 수처럼
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)
보인다.
나는 책인 Stroustrup: The C++ Programming Language (4th Edition) pg 703
에서이 오버로드를 복사했습니다.
누구든지 내가 잘못 한 것을 설명 할 수 있습니까?
하지만'노드 *'에 대한 선행 증가를 오버로드는 – Ashot
좋아, 당신은 설명 할 수 씨 스트로브 스트 룹은 일을 어떻게? 여기에 그 책에 대한 링크가 있습니다. https://www.dropbox.com/s/ipo5pkud6j4vr30/Straustrup4th.pdf?dl=0 – Ashot
@Ashot,이 책은'double ad [] = {1,2,3,4}; double s1 = 누적 (ad, ad + 4,0.0); double s2 = accumulate (ad, ad + 4,0);'. 그것들은 완벽하게 유효한 반복자입니다. –