我正在尝试在一个类中使用 ExprTk 数学表达式解析器库,该类的对象将存储在对象向量中,该对象向量是另一个类的成员变量;但是,当我尝试在向量中推回一个新对象时,我收到很多“使用已删除函数”错误。这是给我带来问题的代码的简单版本:
#include <exprtk.hpp>
#include <iostream>
#include <string>
#include <vector>
class B {
public:
double x;
exprtk::symbol_table<double> symbol_table;
exprtk::parser<double> parser;
exprtk::expression<double> expr_obj;
B();
};
class A {
public:
std::vector<B> Bvec;
A();
};
A::A() {
Bvec.push_back(B());
};
B::B() {
symbol_table.add_variable("x", x);
expr_obj.register_symbol_table(symbol_table);
parser.compile("x^2",expr_obj);
x = 2.0;
std::cout << expr_obj.value() << std::endl;
}
int main(int argc, char const* argv[]) {
A a_obj;
return 0;
}
我没有包含标头库,因为它有近 40,000 行,但可以在这里找到:http://www.partow.net/programming/exprtk/.
这是错误消息
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/string:41,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from src/main.cpp:1:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = B; _Args = {B}; _Tp = B]’:
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = B; _Args = {B}; _Tp = B; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<B>]’
/usr/include/c++/7/bits/vector.tcc:100:30: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {B}; _Tp = B; _Alloc = std::allocator<B>]’
/usr/include/c++/7/bits/stl_vector.h:954:21: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = B; _Alloc = std::allocator<B>; std::vector<_Tp, _Alloc>::value_type = B]’
src/main.cpp:25:21: required from here
/usr/include/c++/7/ext/new_allocator.h:136:4: error: use of deleted function ‘B::B(B&&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.cpp:6:7: note: ‘B::B(B&&)’ is implicitly deleted because the default definition would be ill-formed:
class B {
^
src/main.cpp:6:7: error: ‘exprtk::parser<T>::parser(const exprtk::parser<T>&) [with T = double]’ is private within this context
In file included from src/main.cpp:3:0:
ext_libs/exprtk/exprtk.hpp:35289:7: note: declared private here
parser(const parser<T>&);
^~~~~~
In file included from /usr/include/c++/7/bits/stl_tempbuf.h:60:0,
from /usr/include/c++/7/bits/stl_algo.h:62,
from /usr/include/c++/7/algorithm:62,
from ext_libs/exprtk/exprtk.hpp:37,
from src/main.cpp:3:
/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = B; _Args = {B}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:83:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; bool _TrivialValueTypes = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:134:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*]’
/usr/include/c++/7/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; _Tp = B]’
/usr/include/c++/7/bits/stl_uninitialized.h:311:2: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = B*; _ForwardIterator = B*; _Allocator = std::allocator<B>]’
/usr/include/c++/7/bits/vector.tcc:426:6: required from ‘void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {B}; _Tp = B; _Alloc = std::allocator<B>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<B*, std::vector<B> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = B*]’
/usr/include/c++/7/bits/vector.tcc:105:21: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {B}; _Tp = B; _Alloc = std::allocator<B>]’
/usr/include/c++/7/bits/stl_vector.h:954:21: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = B; _Alloc = std::allocator<B>; std::vector<_Tp, _Alloc>::value_type = B]’
src/main.cpp:25:21: required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘B::B(B&&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Makefile:29: recipe for target 'obj/src/main.o' failed
make: *** [obj/src/main.o] Error 1
我很确定这个问题与push_back函数以及对象的复制方式有关;然而,这有点超出了我的理解范围和 C++ 知识(这是相当基础的)。
如有任何帮助,我们将不胜感激。谢谢!
查看 ExprTk 文档 (readme.txt),特别是第 10.3 节,我们有以下注释:
Note: The exprtk::parser is a non-copyable and non-thread safe component, and should only be shared via either a reference, a shared pointer or a std::ref mechanism, and considerations relating to synchronisation taken into account where appropriate. The parser represents an object factory, specifically a factory of expressions, and generally should not be instantiated solely on a per expression compilation basis.
parser
无法复制,并且根据如何阻止复制的签名,创建了复制构造函数 private
,它也不太可能被移动(private
复制构造函数是您在禁用之前使用的) C++11 中的语言中添加了带有 delete
关键字的特殊成员函数,同时添加了移动语义)。您不能将 exprtk::parser
实例作为要复制的对象的成员(除非您在自定义特殊成员函数中变得非常奇怪并且不复制 exprtk::parser
)。
这都是您不想让相同的
Parser
的多个实例四处浮动的强制措施。您将必须使用引用,很可能是一个智能指针,因为引用是一个但这提出了一个问题:您是否需要保留
parser
作为会员。像这样的事情怎么样:
class B {
public:
double x; // Not sure we need even this.
double result;
B();
};
B::B():
{
// parser is handled with local variables.
exprtk::symbol_table<double> symbol_table;
exprtk::parser<double> parser;
exprtk::expression<double> expr_obj;
symbol_table.add_variable("x", x);
expr_obj.register_symbol_table(symbol_table);
parser.compile("x^2",expr_obj);
x = 2.0;
result = expr_obj.value(); // store instead of printing
} // parser and friends are no longer needed and discarded.