将 exprtk 与自定义类的对象一起使用
Using exprtk with objects of a custom class
我正在尝试学习如何将解析库 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;
}
相关文章:
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 exprtk 与自定义类的对象一起使用
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- 将 std::variant 与 gmock 1.8 对象一起使用时编译错误
- 将 OpenGL 实例化图形与移动对象一起使用
- 如何使用 new 和 delete 与 OpenGL 的缓冲区对象一起使用?
- 如何将指针与对象一起使用;构造函数和破坏者.C
- 有没有一种方法可以将升压信号和插槽与不可复制的对象一起使用
- 有可能实现一个与数组而不是单个对象一起工作的内存池吗
- 如何将自由函数与对象一起使用
- 与对象一起返回时清除了内存指针
- QML:如何将动态创建的组件与自定义的内部对象一起使用
- 将对向量与用户定义的类对象一起使用
- 如何将setw与对象一起使用
- 将 forward_list::p ush_front() 与结构对象一起使用
- 在 C++ lambda 表达式中按值捕获是否需要将值与 lambda 对象一起复制
- 将mem_fun_ref与代理对象一起使用
- 如何将矢量与引用对象一起使用
- MFC:将 GetContextMenuManager()->ShowPopup 与 Office2007 视觉对象一起使用时的延迟
- 与ifsteam对象一起使用.open的问题