C ++和ExprTk解析器“使用已删除的函数”错误

我正在尝试在一个类中使用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,_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,_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,_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>::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 ++(这是非常基础的)知识。

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

tao199107 回答:C ++和ExprTk解析器“使用已删除的函数”错误

Example: df = pd.concat([df1["a"].sum(),df2["a"].sum(),df3["a"].sum(),df4["a"].sum(),df5["a"].sum()]) df out: a a a a a 0 425 425 426 427 425 无法复制,并且基于防止复制的签名,将复制构造函数设置为parser,不太可能将其移动(private复制构造函数是在C ++ 11中使用private关键字禁用特殊成员函数之前使用的语言,同时还添加了移动语义)。不能将delete实例作为要复制的对象的成员(除非您在自定义特殊成员函数中变得很奇怪并且不复制exprtk::parser)。

这是您的全部强制执行,不想让同一exprtk::parser的多个实例随处可见。您将不得不使用一个引用,很可能是smart pointer,因为引用是一个,用于将assign复制到单个实例。

但这提出了一个问题,即您是否需要保留Parser作为成员。那这样的事情呢:

parser
,

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

Section 10.3

本文链接:https://www.f2er.com/1389318.html

大家都在问