c++exprtk

Using exprtk with objects of a custom class


I'm trying to learn how to use the parsing library exprtk with objects of a custom class - I'm still learning C++, tbh but I can write functional non-OO code.

I am trying to follow an example from the repo, but with objects that I'd have created.

Here's my code:

Class definitions:

#include <string>
#include <iostream>
#include <vector>

#include "exprtk_new.hpp"

class myvector2{
private:
    std::vector<float> data;

public:
    myvector2(float a);
    myvector2(float a, float b);
    myvector2(const myvector2& other);
    myvector2 operator+(const myvector2& rhs);
    bool operator==(const myvector2& rhs);
    bool operator<(const myvector2& rhs);
    int size();
};

// constructors
myvector2::myvector2(float a, float b){
    data.push_back(a);
    data.push_back(b);
}

myvector2::myvector2(float a){
    myvector(a, a);
}

myvector2::myvector2(const myvector2& other){
    data.push_back(other.data[0]);
    data.push_back(other.data[1]);
}

// operators
myvector2 myvector2::operator+(const myvector2& rhs){
    return myvector2(data[0]+rhs.data[0], data[1]+rhs.data[1]);
}

bool myvector2::operator==(const myvector2& rhs){
    return (data[0] == rhs.data[0]) & (data[1] == rhs.data[1]);
}

bool myvector2::operator<(const myvector2& rhs){
    if ((data[0] < rhs.data[0]) & (data[1] < rhs.data[1]))
        return true;
    return false;
}

int myvector2::size(){
    return data.size();
}

And the functions to call/test objects of this class (in the same main.cpp file)

template <typename T>
void check_function()
{
    typedef exprtk::symbol_table<T> symbol_table_t;
    typedef exprtk::expression<T>     expression_t;
    typedef exprtk::parser<T>             parser_t;

    T a = T(-1.0, 7.0);
    T b = T(5.0, 4.0);;


    const std::string expression_string = "a + b";


    symbol_table_t symbol_table;
    //symbol_table.add_variable("a", a);
    //symbol_table.add_variable("b", b);
    symbol_table.add_vector("a", a);
    symbol_table.add_vector("b", b);

    expression_t expression;
    expression.register_symbol_table(symbol_table);

    parser_t parser;
    parser.compile(expression_string, expression);

    const T y = expression.value();
}

int main()
{
   //trig_function<double>();
   check_function<myvector2>();
   return 0;
}

And I am running into the following error when attmpting to compile. The line numbers are off because I have some code thats commented out.

1>main.cpp
1>c:\users\prai\perforce\temp\main.cpp(155): error C2664: 'bool exprtk::symbol_table<T>::add_vector(const std::string &,exprtk::vector_view<T> &)': cannot convert argument 2 from 'T' to 'exprtk::vector_view<T> &'
1>        with
1>        [
1>            T=myvector2
1>        ]
1>c:\users\prai\perforce\temp\main.cpp(170): note: see reference to function template instantiation 'void check_function<myvector2>(void)' being compiled
1>c:\users\prai\perforce\temp\main.cpp(156): error C2664: 'bool exprtk::symbol_table<T>::add_vector(const std::string &,exprtk::vector_view<T> &)': cannot convert argument 2 from 'T' to 'exprtk::vector_view<T> &'
1>        with
1>        [
1>            T=myvector2
1>        ]
1>Done building project "exprtk_tester.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I think the issue is that I cannot be using check_function<myvector2>(); since myvector2 is not a type. But how am I supposed to make it work on my specific class myvector2 then?


Solution

  • Turns out you cant pass custom objects - only native datatypes or vectors. Atleast that seems to explain my issues.

    Here is the fixed code that will actually compile and work as expected.

    #include <cstdio>
    #include <string>
    #include <iostream>
    #include <vector>
    
    #include "exprtk_new.hpp"
    
    void check_function()
    {
        typedef exprtk::symbol_table<float> symbol_table_t;
        typedef exprtk::expression<float>     expression_t;
        typedef exprtk::parser<float>             parser_t;
    
        std::vector<float> a{3.0, 7.0, -7.0};
        std::vector<float> b{2.0, 4.0, -2.0};
    
        std::vector<float> c;
        c.resize(3);
    
        const std::string expression_string = "c := a + b";
    
        symbol_table_t symbol_table;
    
        symbol_table.add_vector("a", a);
        symbol_table.add_vector("b", b);
        symbol_table.add_vector("c", c);
    
        expression_t expression;
        expression.register_symbol_table(symbol_table);
    
        parser_t parser;
        if (!parser.compile(expression_string, expression)){
            std::cout << parser.error() <<std::endl;
        }
    
        expression.value();
    }
    
    int main()
    {
       check_function();
       return 0;
    }