返回在堆 (C++) 上分配的类的实例



我目前正在处理的程序编写得像一个脚本,除了我正在使用(或试图)将 3 个变量相互关联的 1 个类。我有一个名为"SupNetModel"的类,它包含 3 个不同的值,没有类方法。此类声明为头文件,因为实际上没有任何方法可以为它实现。

#ifndef SUPNETMODEL_H
#define SUPNETMODEL_H
class SupNetModel {
    public:
        //variables
        SupNetModel();
};
#endif

我的主函数的精简版本如下所示:

#include "train.h"
#include "SupNetModel.h"
int main() {
    //Some stuff
    SupNetModel* model = Train(data,labels);
    return 1;
}

火车.cpp如下所示

#include "train.h"
SupNetModel* Train(arma::mat data,  arma::mat labels) {
    SupNetModel * model = new SupNetModel();
    //Do a bunch of stuff
    return model;
}

编辑:Train.h看起来像:

#ifndef TRAIN_H
#define TRAIN_H
#include <RcppArmadillo.h>
#include "SupNetModel.h"
SupNetModel* Train(arma::mat data, arma::mat labels);
#endif

所以本质上我要做的是在堆上创建一个我的类的实例,然后将指针返回到该实例,以便我可以在 main 中访问它。但是,目前我收到以下错误"'SupNetModel'没有命名类型"

如果相关,则在 R 环境中使用 RCpp 构建。错误引用的错误行位于 RcppExports 中.cpp它是自动生成的。在仔细阅读该文件(其中包含有关类的信息)后,除了在Train的函数声明行中之外,SupNetModel似乎没有出现在RcppExports中。

编辑:这是RCppExports文件中相关的部分:

// train
SupNetModel* train(arma::mat data, arma::mat labels);
RcppExport SEXP SupervisedNetworks_train(SEXP dataSEXP, SEXP labelsSEXP) {
BEGIN_RCPP
    SEXP __sexp_result;
    {
        Rcpp::RNGScope __rngScope;
        Rcpp::traits::input_parameter< arma::mat >::type data(dataSEXP );
        Rcpp::traits::input_parameter< arma::mat >::type labels(labelsSEXP );
        SupNetModel* __result = trainNetworkOfNetworks(data, labels);
        PROTECT(__sexp_result = Rcpp::wrap(__result));
    }
    UNPROTECT(1);
    return __sexp_result;
END_RCPP

}

以下是我尝试构建项目时出现的最后几行:

g++ -I/usr/share/R/include -DNDEBUG -I"/home/anne/LabWork/qpOASES-3.0.0/include" -std=c++11   -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/home/anne/R/x86_64-pc-linux-gnu-library/3.1/RcppArmadillo/include"   -fpic  -O3 -pipe  -g  -c LS_LocalLaplacian.cpp -o LS_LocalLaplacian.o
g++ -I/usr/share/R/include -DNDEBUG -I"/home/anne/LabWork/qpOASES-3.0.0/include" -std=c++11   -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/home/anne/R/x86_64-pc-linux-gnu-library/3.1/RcppArmadillo/include"   -fpic  -O3 -pipe  -g  -c RcppExports.cpp -o RcppExports.o
RcppExports.cpp:241:1: error: ‘SupNetModel’ does not name a type
 SupNetModel* train(arma::mat data, arma::mat labels);
 ^
RcppExports.cpp: In function ‘SEXPREC* SupervisedNetworks_train(SEXP, SEXP)’:
RcppExports.cpp:249:9: error: ‘SupNetModel’ was not declared in this scope
     SupNetModel* __result = train(data, labels);
         ^
RcppExports.cpp:249:22: error: ‘__result’ was not declared in this scope
     SupNetModel* __result = train(data, labels);
                  ^
RcppExports.cpp:249:68: error: ‘train’ was not declared in this scope
     SupNetModel* __result = train(data, labels);
                                                                ^
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for package ‘SupervisedNetworks’
* removing ‘/home/anne/R/x86_64-pc-linux-gnu-library/3.1/SupervisedNetworks’
* restoring previous ‘/home/anne/R/x86_64-pc-linux-gnu-library/3.1/SupervisedNetworks’
Exited with status 1.

是什么导致了此错误,您认为如何解决?

看起来解决方案非常简单。

测试train()函数遗留下来的,我在train函数上方留下了//[[Rcpp::export]]标签,它允许R访问train()。

最新更新