I'm new to C++ programming, trying to experiment with Rcpp through R. I created a function to produce all possible k-mers from a string. It works well in the serial form of it:
#include <Rcpp.h>
#include <string>
#include <iostream>
#include <ctime>
// using namespace Rcpp;
// [[Rcpp::export]]
std::vector< std::string > cpp_kmer( std::string s, int k ){
std::vector< std::string > kmers;
int seq_loop_size = s.length() - k+1;
for ( int z=0; z < seq_loop_size; z++ ) {
std::string kmer;
kmer = s.substr( z, k );
kmers.push_back( kmer ) ;
}
return kmers;
}
However, when I try to use this function in a parallel implementation (using RcppParallel), with the code below:
#include <Rcpp.h>
#include <string>
#include <iostream>
#include <ctime>
using namespace Rcpp;
// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
using namespace RcppParallel;
struct p_cpp_kmer : public Worker {
// input string
std::vector< std::string > seqs;
int k;
std::vector< std::string > cpp_kmer( std::string s, int k );
// destination list
List output;
std::string
sub_s;
// initialize with source and destination
p_cpp_kmer(std::vector< std::string > seqs, int k, List output)
: seqs(seqs), k(k), output(output) {}
// calculate k-mers for the range of sequences requested
void operator()(std::size_t begin, std::size_t end) {
for (std::size_t i = begin; i < end; i++)
sub_s = seqs[i];
cpp_kmer(sub_s, k);
}
};
// [[Rcpp::export]]
List par_cpp_kmer(std::vector< std::string > seqs, int k, bool v){
// allocate output list
List outpar(num_seqs);
int num_seqs = seqs.size();
// p_cpp_kmer functor (pass input and output matrixes)
p_cpp_kmer par_kmer(seqs, k, outpar);
parallelFor(0, num_seqs, par_kmer);
return wrap(outpar);
}
std::vector< std::string > cpp_kmer( std::string s, int k ){
std::vector< std::string > kmers;
int seq_loop_size = s.length() - k+1;
for ( int z=0; z < seq_loop_size; z++ ) {
std::string kmer;
kmer = s.substr( z, k );
kmers.push_back( kmer ) ;
}
return kmers;
}
It fails to compile, giving an: undefined reference to p_cpp_kmer::cpp_kmer(std::string, int)' error.
I know it has to do with declaring/referencing the cpp_kmer, but I just can't figure out where/how to do so appropriately (due to my lack of knowledge in C++).
Thank you very much in advance.