4

我正在尝试在一个类中使用 ExprTk 数学表达式解析器库,该类的对象将存储在对象向量中,该向量是另一个类的成员变量;但是,当我尝试 push_back 向量中的新对象时,我遇到了很多“使用已删除函数”错误。这是给我带来问题的代码的简单版本:

#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++ 的知识(这是相当基本的)。

任何帮助,将不胜感激。谢谢!

4

2 回答 2

3

查看 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.

第 10.3 节

于 2020-10-17T19:04:56.680 回答
2

parser不能被复制,并且基于如何防止复制的签名,复制构造函数被创建private,它也不太可能被移动(private复制构造函数是你在禁用带有delete关键字的特殊成员函数之前使用的东西被添加到C++11 中的语言,同时添加了移动语义)。您不能将exprtk::parser实例作为要复制的对象的成员(除非您在自定义特殊成员函数中变得非常奇怪并且不复制exprtk::parser.

这是您不想让同一个实例的多个实例Parser四处浮动的所有强制措施。您将不得不使用一个引用,很可能是一个智能指针,因为引用是一个 <expletive deleted> 复制分配,而不是单个实例。

但这提出了一个问题,即您是否需要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.
于 2020-10-15T19:37:26.207 回答