将 exprtk 与自定义类的对象一起使用

Using exprtk with objects of a custom class

本文关键字:对象 一起 exprtk 自定义      更新时间:2023-10-16

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

我正在尝试遵循存储库中的示例,但使用我会创建的对象。

这是我的代码:

类定义:

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

以及调用/测试此类对象的函数(在同一个主.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:userspraiperforcetempmain.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:userspraiperforcetempmain.cpp(170): note: see reference to function template instantiation 'void check_function<myvector2>(void)' being compiled
1>c:userspraiperforcetempmain.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<myvector2>();因为 myvector2 不是一种类型。但是我应该如何让它在我的特定班级myvector2上工作呢?

事实证明,您无法传递自定义对象 - 只能传递本机数据类型或向量。至少这似乎解释了我的问题。

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

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