vendredi 23 octobre 2020

Calling GNU Octave from C++ (C++ newbie)

I'm new to C++, but am trying to write a function that embeds the octave interpreter in C++, as described here https://octave.org/doc/v4.0.1/Standalone-Programs.html .

I'm trying to do this from a program that I'm writing in Eclipse, and trying to compile with gcc (on linux). I want to be able to call an external script, as in the second example in the link.

My code so far looks like this.....


#include <iostream>
#include <oct.h>
#include <octave.h>
#include <parse.h>
#include <interpreter.h>

using namespace std;

class OctaveInt {

public:
    void callOctave (double, int, string);
    OctaveInt(string path );

private:
    octave::interpreter interpreter;
};

// Member functions including constructor..
OctaveInt::OctaveInt(string path)
    {
    // Constructor - initialises engine and sets path
    int status = interpreter.execute();
    octave_value_list p;
    p(0) = path;
    octave_value_list o1 = octave::feval ("addpath", p);
    }


void OctaveInt::callOctave(double params, int size, string name) {

    std::cout << "Hello World" << std::endl;

    int n = 2;

    octave_value_list in;
    octave_value_list p;

    for (octave_idx_type i=0; i < size; i++)
        in(i) = octave_value(params[i]);


    octave_value_list out = octave::feval (name, in);

    std::cout << "Output is ";
    std::cout << out(0).int_value();

}


int main() {

    double params[] = {100, 2, 3, 4, 5, 6};
    int size = 6;
    string path = "/home/arwel/eclipseWorkspace_new/octaveCaller/src/";
    OctaveInt octI(path);
    octI.callOctave(params, size, "myFunction");

    return 0;

}

When I try to compile however, I get a series of errors.....

Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/include/octave-5.2.0/octave/ -I/usr/share/octave/5.2.0/etc/tests -I/usr/lib/x86_64-redhat-linux6E -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/octaveCaller.d" -MT"src/octaveCaller.o" -o "src/octaveCaller.o" "../src/octaveCaller.cpp"
../src/octaveCaller.cpp: In constructor ‘OctaveInt::OctaveInt(std::string)’:
../src/octaveCaller.cpp:31:6: warning: unused variable ‘status’ [-Wunused-variable]
  int status = interpreter.execute();
      ^
../src/octaveCaller.cpp: In member function ‘void OctaveInt::callOctave(double, int, std::string)’:
../src/octaveCaller.cpp:48:32: error: invalid types ‘double[octave_idx_type {aka long int}]’ for array subscript
   in(i) = octave_value(params[i]);
                                ^
../src/octaveCaller.cpp:42:6: warning: unused variable ‘n’ [-Wunused-variable]
  int n = 2;
      ^
../src/octaveCaller.cpp: In function ‘int main()’:
../src/octaveCaller.cpp:65:44: error: no matching function for call to ‘OctaveInt::callOctave(double [6], int&, const char [11])’
  octI.callOctave(params, size, "myFunction");
                                            ^
../src/octaveCaller.cpp:65:44: note: candidate is:
../src/octaveCaller.cpp:38:6: note: void OctaveInt::callOctave(double, int, std::string)
 void OctaveInt::callOctave(double params, int size, string name) {
      ^
../src/octaveCaller.cpp:38:6: note:   no known conversion for argument 1 from ‘double [6]’ to ‘double’
make: *** [src/octaveCaller.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

12:41:36 Build Failed. 3 errors, 2 warnings. (took 1s.927ms)

So it looks like I have some problems with types of variables (??). As the title of the post suggests, I don't really know much C++, so I'm undoubtedly doing some basic C++ mistake. Can someone give me a hand to figure out what stupid thing I'm doing? Cheers, Arwel

Aucun commentaire:

Enregistrer un commentaire