【问题标题】:Using exprtk with objects of a custom class将 exprtk 与自定义类的对象一起使用
【发布时间】:2020-02-17 04:35:51
【问题描述】:

我正在尝试学习如何将解析库 exprtk 与自定义类的对象一起使用 - 我仍在学习 C++,但我可以编写功能性非 OO 代码。

我正在尝试关注 example from the repo,但使用的是我已经创建的对象。

这是我的代码:

类定义:

#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();
}

以及调用/测试此类对象的函数(在同一个 main.cpp 文件中)

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;
}

我在尝试编译时遇到了以下错误。行号已关闭,因为我有一些代码被注释掉了。

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 ==========

我认为问题在于我不能使用check_function&lt;myvector2&gt;();,因为 myvector2 不是类型。但是我应该如何让它在我的特定课程myvector2 上工作呢?

【问题讨论】:

    标签: c++ exprtk


    【解决方案1】:

    原来你不能传递自定义对象——只能传递原生数据类型或向量。至少这似乎可以解释我的问题。

    这是实际编译并按预期工作的固定代码。

    #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;
    }
    
    

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2013-04-17
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      相关资源
      最近更新 更多