I have just finished a course on c++ at university, and we got our final assignment. (I just want to clearify: this is in no way me asking you to do my homework for me, I just need some help with the code.)
Our first part of the assignment is to write a generic function that gets a functor and 2 indices: the functor is a condition, and the indices point to the first element in a container and the place after the last element in a container. The function needs to check how many pairs of elements in the container meet the condition of the functor.
This is my code:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
bool test(vector<int> v);
template<typename InputIterator, typename Condition>
int checkCondition(InputIterator first, InputIterator last, Condition condition);
class bigger{
public:
bigger() = default;
~bigger = default();
bigger(const bigger&) = default;
bigger& operator=(const bigger&) = default;
bool operator()(int a, int b) const {
return (a<b);
}
};
template<typename InputIterator, typename Condition>
int checkCondition(InputIterator first, InputIterator last, Condition condition){
int counter = 0;
while (first!=last){
vector<int>::iterator temp = first;
++temp;
if (condition(*first, *temp)){
counter++;
}
++first;
}
return counter;
}
bool test(vector<int> v){
vector<int>::iterator first = v.begin();
vector<int>::iterator last = v.end();
bigger condition();
return checkCondition(first, last, condition);
}
And here is the error that I get by compiling with g++ using Linux's terminal (ubuntu because I'm using windows):
part1OfDry.cpp: In instantiation of ‘int checkCondition(InputIterator, InputIterator, Condition) [with InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; Condition = bigger (*)()]’:
part1OfDry.cpp:41:49: required from here
part1OfDry.cpp:29:22: error: too many arguments to function
if (condition(*first, *temp)){
~~~~~~~~~^~~~~~~~~~~~~~~
part1OfDry.cpp:29:22: error: could not convert ‘condition()’ from ‘bigger’ to ‘bool’
And this is my compilation line:
g++ -o test1.exe -std=c++11 -Wall -pedantic-errors -Werror -DNDEBUG thing.cpp
If someone can please explain why the errors are happening, I'll be very thankful.