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

标签 c++ parameter-passing heap-memory rcpp armadillo

<分区>

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

#ifndef SUPNETMODEL_H
#define SUPNETMODEL_H

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

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

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

Train.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 中。细读该文件(其中包含有关类的信息)后,SupNetModel 似乎没有出现在 RcppExports 中,除了在 Train 的函数声明行中。

编辑:这是 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() 。删除它处理了错误!

关于c++ - 返回在堆上分配的类的实例 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070618/

相关文章:

python - 如何在 python 中检查指定函数的至少一个默认参数

delphi - 如何获取delphi函数中参数传递的变量或对象节点的名称?

java - Tomcat 6 堆大小 - 这是正确的吗?

c - 有什么方法可以防止应用程序在堆损坏时崩溃? - C 编程语言

c++ - 如何使用 boost::interprocess::vector 在共享内存中分配复杂的结构?

c++ - 如何在 C/C++ 中使用 lame 将 mp3 解码为 wav?

android - Android 上的 Qt/QML - onReadingChanged

java - 图像处理 - OpenCV,识别数字

c# - 如何在另一个类中传递不同的类对象并期望它们得到类似的对待?

android - 无法增加 Android Studio 的内存堆