0

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.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Gil Levin
  • 41
  • 5

2 Answers2

4

Try writing

bigger condition{};

or, as suggested by 0x5453, simply

bigger condition;

instead of

bigger condition();

Problem: as you can see from the error message, bigger condition(); is interpreted

 Condition = bigger (*)()

as declaration of a function returning a bigger.

Search for "most vexing parse" for clarifications.

max66
  • 65,235
  • 10
  • 71
  • 111
  • Or simply `bigger condition;`. – 0x5453 Feb 06 '20 at 14:20
  • @0x5453 - yes, it's simpler; added; thanks. – max66 Feb 06 '20 at 14:24
  • His default destructor also has extra brackets – Adrian Cornish Feb 06 '20 at 14:24
  • @AdrianCornish - I didn't seen it... not extra brackets but brackets in the wrong place. IMHO, you should explain, in your answer, the exact place were the parentheses are wrong. – max66 Feb 06 '20 at 14:31
  • @max66 Which is why I posted the code - if you cannot diff two files then what can I do? Plus I saw it because I actually tried compiling his code which you obviously did not do – Adrian Cornish Feb 06 '20 at 14:32
  • @AdrianCornish - I think you could simply write the wrong line (`~bigger = default();`), explaining that is wrong and why, and propose the correct line. – max66 Feb 06 '20 at 14:34
  • @max66 Or I could post the complete correct code? What is you point Max? – Adrian Cornish Feb 06 '20 at 14:36
  • 1
    @AdrianCornish - the point is that from your answer I don't understand were are the not needed parentheses; from your first comment I understand it. IMHO, your first comment is a better answer than your answer. – max66 Feb 06 '20 at 14:38
-1

Stop adding parentheses where they are not needed.

#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);
}
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77