I am practicing C++ code snippets in the CPP Crash Course Book (Chapter-8, Exercise 1), Link the below does not compile and the error of the program has been provided below. It would be helpful if any suggestions or links for understanding is provided. :).
#include <cstdio>
struct FibonacciIterator {
bool operator!=(int x) const {
return x >= current;
}
FibonacciIterator& operator++() {
const auto tmp = current;
current += last;
last = tmp;
return *this;
}
int operator*() const {
return current;
}
private:
int current{ 1 }, last{ 1 };
};
struct FibonacciRange {
explicit FibonacciRange(int max) : max{ max } { }
FibonacciIterator begin() const {
return FibonacciIterator{};
}
int end() const {
return max;
}
private:
const int max;
};
int main() {
for (const auto i : FibonacciRange{ 50 }) {
printf("%d ", i);
}
}
Error:
user@stretch:/tmp$ g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:32:46: error: inconsistent begin/end types in range-based ‘for’ statement: ‘FibonacciIterator’ and ‘int’
for (const auto i : FibonacciRange{ 5000 }) {
^
test.cpp:32:46: error: conversion from ‘int’ to non-scalar type ‘FibonacciIterator’ requested
test.cpp:32:46: error: no match for ‘operator!=’ (operand types are ‘FibonacciIterator’ and ‘FibonacciIterator’)
test.cpp:3:10: note: candidate: bool FibonacciIterator::operator!=(int) const
bool operator!=(int x) const {
^~~~~~~~
test.cpp:3:10: note: no known conversion for argument 1 from ‘FibonacciIterator’ to ‘int’
Aucun commentaire:
Enregistrer un commentaire