mex-it icon indicating copy to clipboard operation
mex-it copied to clipboard

returning a std::pair

Open krober10nd opened this issue 5 years ago • 0 comments

Hello, I am trying to mex a function that ideally would return a std::pair but when I go to mex from MATLAB I receive a number of errors related to the return of this type. Any help would be appreciated! Below is the code and below that is the error I get from MATLAB

//Find unique pairs of integers #include #include #include

bool ArePairsUnique(const std::pair<int,int>& a, const std::pair<int,int>& b){ if(a.first == b.first && a.second == b.second) { return 1; // not unique } else{ return 0; // unique } }

bool compare(const std::pair<int,int> &i, const std::pair<int,int> &j) { return i.first < j.first; }

std::vector< std::pair<int,int> > findUniqueEdges(const std::vector& e1, const std::vector& e2, const int& nedges){

// Declaring vector of pairs
std::vector< std::pair<int,int> > uniqueEdges;
uniqueEdges.reserve(nedges);

// Allocate
// Entering values in vector of pairs
for (int i = 0; i < nedges; i++)
    uniqueEdges.push_back(std::make_pair(e1[i], e2[i]));

// First column has the larger number
for (int i = 0; i < nedges; i++)
    uniqueEdges[i] =  uniqueEdges[i].first < uniqueEdges[i].second ? std::make_pair(uniqueEdges[i].second,uniqueEdges[i].first) : uniqueEdges[i];

// Sort pairs of integers by first column using compare()
std::sort(uniqueEdges.begin(), uniqueEdges.end(), compare);

// Remove duplicate edges
auto ip = std::unique(uniqueEdges.begin(),uniqueEdges.end(),ArePairsUnique);

// Give back to the user unique edges only
uniqueEdges.resize(std::distance(uniqueEdges.begin(), ip));

return uniqueEdges;

}

// mex it up for usage in matlab // first args are inputs, last arg is output void mex_function(const std::vector& e1, const std::vector& e2, const int& nedges, std::vector< std::pair<int,int> > uniqueEdges ) {

// this is how you call the function from matlab
uniqueEdges = findUniqueEdges(e1,e2,nedges);

}

#include "mex-it.h"

In file included from /Users/Keith/junk/FindUniqueBars/test_sortuniquebars.cpp:52: /Users/Keith/junk/FindUniqueBars/mex-it.h:531:13: error: assigning to 'type' (aka 'double') from incompatible type 'const std::__1::__vector_base<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > >::value_type' (aka 'const std::__1::pair<int, int>') *mat++ = item[i]; ^~~~~~~ /Users/Keith/junk/FindUniqueBars/mex-it.h:863:4: note: in instantiation of function template specialization 'mex_binding::assign_to_matlab<std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > > >' requested here assign_to_matlab(array[arg_idx], std::get<I>(args)); ^ /Users/Keith/junk/FindUniqueBars/mex-it.h:866:3: note: in instantiation of function template specialization 'mex_binding::assign_args<void (const std::__1::vector<int, std::__1::allocator &, const std::__1::vector<int, std::__1::allocator > &, const int &, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > >), 3, 4, std::__1::tuple<std::__1::vector<int, std::__1::allocator >, std::__1::vector<int, std::__1::allocator >, int, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > > > >' requested here assign_args<funct,I+1,N>(array,arg_idx,args); ^ /Users/Keith/junk/FindUniqueBars/mex-it.h:866:3: note: in instantiation of function template specialization 'mex_binding::assign_args<void (const std::__1::vector<int, std::__1::allocator &, const std::__1::vector<int, std::__1::allocator > &, const int &, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > >), 2, 4, std::__1::tuple<std::__1::vector<int, std::__1::allocator >, std::__1::vector<int, std::__1::allocator >, int, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > > > >' requested here /Users/Keith/junk/FindUniqueBars/mex-it.h:866:3: note: in instantiation of function template specialization 'mex_binding::assign_args<void (const std::__1::vector<int, std::__1::allocator &, const std::__1::vector<int, std::__1::allocator > &, const int &, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > >), 1, 4, std::__1::tuple<std::__1::vector<int, std::__1::allocator >, std::__1::vector<int, std::__1::allocator >, int, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > > > >' requested here /Users/Keith/junk/FindUniqueBars/mex-it.h:897:4: note: in instantiation of function template specialization 'mex_binding::assign_args<void (const std::__1::vector<int, std::__1::allocator &, const std::__1::vector<int, std::__1::allocator > &, const int &, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > >), 0, 4, std::__1::tuple<std::__1::vector<int, std::__1::allocator >, std::__1::vector<int, std::__1::allocator >, int, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > > > >' requested here assign_args<R(Args...),0,sizeof...(Args)>(plhs,i,Local_Args); ^ /Users/Keith/junk/FindUniqueBars/mex-it.h:923:11: note: in instantiation of member function 'mex_binding::call_mex_helper<void (const std::__1::vector<int, std::__1::allocator > &, const std::__1::vector<int, std::__1::allocator > &, const int &, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > )>::call_wrapper' requested here helper.call_wrapper(nlhs, plhs, nrhs, prhs); ^ /Users/Keith/junk/FindUniqueBars/mex-it.h:981:18: note: in instantiation of function template specialization 'mex_binding::call_mex_function<void (const std::__1::vector<int, std::__1::allocator > &, const std::__1::vector<int, std::__1::allocator > &, const int &, std::__1::vector<std::__1::pair<int, int>, std::__1::allocator<std::__1::pair<int, int> > >)>' requested here mex_binding::call_mex_function(mex_function, nlhs, plhs, nrhs, prhs); ^ 1 error generated.

krober10nd avatar Nov 10 '19 23:11 krober10nd