Hi,

I am trying to use the "partition" from STL algorithms. I get the following error:

error: no matching function for call to ‘partition(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, <unresolved overloaded function type>)’
/usr/include/c++/4.2.1/bits/stl_algo.h:2098: note: candidates are: _ForwardIterator std::partition(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Predicate = bool (test::*)(int)]

I am attaching part of my code where I have used partition. I have included the following three libraries:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;


bool test::mypred(int i) 
{
	if(flag_equiv[i] == 1)
	return true;
	else
	return false;
}

void test::function1
{
                for(int i=0; i<(int)temp_myvec1_col.size(); i++)	
		{
			if(temp_myvec1_col[i].size() > 1)
			{
				counter_temp_equiv = 0;
				flag_equiv[0] = 0;				
				for(int lk=1; lk<(int)temp_myvec1_col[i].size(); lk++)
				{
					flag_equiv[lk] = 0;
					std::map<int,int>::iterator fbg=equiv_pair.find(temp_myvec1_col[i][lk]);
					if(fbg != equiv_pair.end())
					{
						flag_equiv[lk] = 0;				
					}
					else
					{
						flag_equiv[lk] = 1;
					}
				}
				bound = partition(temp_myvec1_col[i].begin(), temp_myvec1_col[i].end(), mypred);
				
				if(bound != temp_myvec1_col[i].end() && bound != temp_myvec1_col[i].begin())
				counter_temp_equiv++;			

				if(counter_temp_equiv > 0)
				{
					for(it_equiv=temp_myvec1_col[i].begin(); it_equiv!=bound; ++it_equiv)
					{
						temp_equiv_vec_row.push_back(*it_equiv);
					}
					temp_equiv_vec_col.push_back(temp_equiv_vec_row);
					temp_equiv_vec_row.clear();

					for(it_equiv=bound; it_equiv!=temp_myvec1_col[i].end(); ++it_equiv)
					{
						temp_equiv_vec_row.push_back(*it_equiv);
					}
					temp_equiv_vec_col.push_back(temp_equiv_vec_row);
					temp_equiv_vec_row.clear();
				}

			}
		}
}

Thanks

Is test::mypred a static member function of your class test?

If not then this wont work, you can not pass a class member as a callback like that because the called code in std::partition has no object test on which to call the callback function.

To access the data in your function you need to write a predicate functor (an object that acts like a function) to allow access to your data.

Unfortunately since you have failed to post most of the declarations of all the symbols you use it is quite hard to give an example but assuming that temp_myvec1_col is declard something like vector<vector<int> > temp_myvec1_col; it would be something like

class myPredicate
{
public:
    myPredicate(test& aTest)
    : theTest(aTest)
    {
    }

    bool operator()(int i) 
    {
	if(theTest.flag_equiv[i] == 1)
	    return true;
	else
	    return false;
    }

private:
    test& theTest;
}

assuming that flag_equiv had public access or that myPredicate was a friend of test and you would call it something like

bound = partition(temp_myvec1_col[i].begin(), temp_myvec1_col[i].end(), myPredicate(*this));

myPredicate(*this) creates an instance of the class myPredicate with a reference to the current instance of test held within it. Since it implements operator() this instance of myPredicate can be called just like a function so when it is called operator() has access to the calling parameters and the data members of myPredicate allowing it access to the data in the instance of test.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.