我在C ++中具有以下代码,该代码保存在名为“ code.cpp”的文件中:
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <limits>
unsigned long readULong(std::string str) {
std::istringstream stream(str);
unsigned long i;
stream >> i;
return i;
}
void usage(std::string cmd) {
std::cout << "-----" << std::endl;
std::cout << "LEAST SQUARES CLUSTERING PROCEDURE" << std::endl;
std::cout << "By David B. Dahl" << std::endl;
std::cout << "For reference, algorithmic details, source code, and licensing,
see:" << std::endl;
std::cout << "http://dahl.byu.edu/software/lsclust.zip" << std::endl;
std::cout << "-----" << std::endl;
std::cout << "usage: " << cmd << " [ -p ] candidates-filename number-of-
candidates number-of-items" << std::endl;
std::cout << "-----" << std::endl;
exit(1);
}
int main(int argc, char **argv) {
if ( argc < 2 ) usage(argv[0]);
bool printMatrix = false;
unsigned offset = 0;
if ( std::string(argv[1]) == "-p" ) {
printMatrix = true;
offset++;
}
if ( argc-offset != 4 ) usage(argv[0]);
unsigned long nClusterings(readULong(argv[offset+2]));
unsigned long nItems(readULong(argv[offset+3]));
unsigned int **b = new unsigned int*[nClusterings];
for ( unsigned long c=0; c<nClusterings; c++ ) {
b[c] = new unsigned int[nItems];
}
std::ifstream clusteringFile;
clusteringFile.open(argv[offset+1]);
unsigned int label;
unsigned long count = 0;
while ( clusteringFile >> label ) {
b[ count / nItems ][ count % nItems ] = label;
count++;
}
clusteringFile.close();
assert( count == nClusterings * nItems );
double **pp = new double*[nItems];
for ( unsigned long i=0; i<nItems; i++ ) {
pp[i] = new double[i];
for ( unsigned long j=0; j<i; j++ ) {
pp[i][j] = 0.0;
}
}
for ( unsigned long c=0; c<nClusterings; c++ ) {
for ( unsigned long i=0; i<nItems; i++ ) {
for ( unsigned long j=0; j<i; j++ ) {
if ( b[c][i] == b[c][j] ) pp[i][j]++;
}
}
}
for ( unsigned long i=0; i<nItems; i++ ) {
for ( unsigned long j=0; j<i; j++ ) {
pp[i][j] /= (double)nClusterings;
}
}
double minDB = std::numeric_limits<double>::max();
unsigned long minIndex = 0;
for ( unsigned long c=0; c<nClusterings; c++ ) {
double db = 0.0;
for ( unsigned long i=0; i<nItems; i++ ) {
for ( unsigned long j=0; j<i; j++ ) {
double x = ( b[c][i] == b[c][j] ? 1 : 0 ) - pp[i][j];
db += x*x;
}
}
if ( db < minDB ) {
minDB = db;
minIndex = c;
}
}
std::cout << b[minIndex][0];
for ( unsigned long i=1; i<nItems; i++ ) {
std::cout << " " << b[minIndex][i];
}
std::cout << std::endl;
// std::cout << minDB << std::endl;
if ( printMatrix ) {
char buffer[50];
for ( unsigned long i=0; i<nItems; i++ ) {
for ( unsigned long j=0; j<i; j++ ) {
sprintf(buffer,"%4.2f ",pp[i][j]);
std::cerr << buffer;
}
std::cerr << "1.00" << std::endl;
}
}
return 0;
}
我想使用“ sourceCpp”命令通过Rcpp在R中运行代码。为此,我在代码中添加了以下命令:#include <Rcpp.h>
之前的#include <string>
,using namespace Rcpp
之后的#include <limits>
和//[[Rcpp::export]]
之前的int main(int argc, char **argv){
因为main
是我要返回的函数。但是,我收到以下错误消息:
cannot convert SEXP to Rcpp::traits::input_parameter<char>::type** {aka
Rcpp::InputParameter<char>**} in initialization
和
invalid user-defined conversion from Rcpp::traits::input_parameter<char>::type
{aka Rcpp::InputParameter<char>} to char** [-fpermissive]
我在做什么错了?