"error: cannot convert ‘Funct’ to ‘int’ in initialization" when trying to use functors.

I'm trying to get the hang of functors but I get this error on line 18.
I'd really appreciate it if someone could tell me where I've gone wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <iostream>
#include <algorithm>

class Funct
{
		int Total;
	public:
		Funct() : Total(0) {};
		void operator() (int& i )
		{
			Total += i;
		}
} funct;

int main()
{
	std::vector<int> v = {1,5};
	int sum = std::for_each(v.begin(),v.end(),funct); //Error here
	std::cout<<"The total is "<<sum<<".\n";
}
You shall use funct()
Oops, I am sorry. I did not see that you already defined the object.
The problem is that your operator function does not return anything. And you are trying assign a pointer to fucntion to int. You can redefine your class the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Funct
{
		int Total;
	public:
		Funct() : Total(0) {};
		void operator() (int& i )
		{
			Total += i;
		}
		int get_total() const
		{
			return ( Total );
		}

} funct;


And then call

1
2
std::for_each(v.begin(),v.end(),funct); 
int sum = func.get_total();


Or

int sum = std::for_each(v.begin(),v.end(),funct).get_total();
Last edited on
I used funct() but then it said that the function expects one argument and no arguments have been provided.
I have updated my message.
Thanks, that worked but I get incorrect result.
Instead of showing a total of 6, it shows a total of 0.
It is my mistake. Shall be

int sum = std::for_each(v.begin(),v.end(),funct).get_total();

I made the mistake because I thought that Total was defined as static member.
Last edited on
Thanks, that worked but i'm confused.
According to the documentation, std::for_each has the same return type as the functor.
our function returned void, so how did we call the get_total() method on it?
Would this code be considered acceptable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <iostream>
#include <algorithm>

class Funct
{
		int mTotal;
	public:
		Funct() : mTotal(0) {};

		int operator() (int& i )
		{
			return (mTotal += i);
		}
	

} funct;

int main()
{
	std::vector<int> v = {1,5};
	int sum = std::for_each(v.begin(),v.end(),funct);
	std::cout<<"Sum: "<<sum<<".\n";
}

According to the documentation, std::for_each has the same return type as the functor.
our function returned void, so how did we call the get_total() method on it?


The algorithm returns what was passed to it. As you are passing the object it is returned. If you would pass a pointer to function it were returned.

Your last code shall not be compiled because the object is returned.
You could change your class the following way adding the conversion operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Funct
{
		int mTotal;
	public:
		Funct() : mTotal(0) {};

		int operator() (int& i )
		{
			return (mTotal += i);
		}
	
		operator int() const
		{
			return ( mTotali);
		}

} funct;


In this case it shall work.
Last edited on
int sum = std::accumulate(v.begin(), v.end(), 0);
@vlad from moscow. That's brilliant. Thansk a lot for your help :-)
Topic archived. No new replies allowed.