为什么`string` `vector` 等在野牛的联合中不起作用?

问题描述

我试图为 C 语言的一个子集编写解析器。但是当我在 string 中包含 vector%union 时,它会抛出 error 。使用 char*array 似乎没问题。

%union{
    string s;
    vector<int>v;
}

我在某些地方看到它与标题问题有关,他们建议将其包括在内

%code requires{
    #include <string>
}

对于字符串大小写。但这对我也不起作用。

我得到的错误

y.tab.c:1174:9: error: use of deleted function ‘YYSTYPE::YYSTYPE()’
 YYSTYPE yylval;
         ^~~~~~
y.tab.c:223:7: note: ‘YYSTYPE::YYSTYPE()’ is implicitly deleted because the default deFinition would be ill-formed:
 union YYSTYPE
       ^~~~~~~
demo.y:48:12: error: union member ‘YYSTYPE::s’ with non-trivial ‘std::__cxx11::basic_string<_CharT,_Traits,_Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
     string s;
            ^
demo.y:49:16: error: union member ‘YYSTYPE::v’ with non-trivial ‘std::vector<_Tp,_Alloc>::vector() [with _Tp = int; _Alloc = std::allocator<int>]’
     vector<int>v;
                ^
y.tab.c: In function ‘int yyparse()’:
y.tab.c:1203:30: error: use of deleted function ‘YYSTYPE::YYSTYPE()’
     YYSTYPE yyvsa[YYINITDEPTH];
                              ^
y.tab.c:1203:30: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
y.tab.c:223:7: note: ‘YYSTYPE::~YYSTYPE()’ is implicitly deleted because the default deFinition would be ill-formed:
 union YYSTYPE
       ^~~~~~~
demo.y:48:12: error: union member ‘YYSTYPE::s’ with non-trivial ‘std::__cxx11::basic_string<_CharT,_Alloc>::~basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
     string s;
            ^
demo.y:49:16: error: union member ‘YYSTYPE::v’ with non-trivial ‘std::vector<_Tp,_Alloc>::~vector() [with _Tp = int; _Alloc = std::allocator<int>]’
     vector<int>v;
                ^
y.tab.c:1203:30: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
     YYSTYPE yyvsa[YYINITDEPTH];
                              ^
y.tab.c:1215:11: error: use of deleted function ‘YYSTYPE::YYSTYPE()’
   YYSTYPE yyval;
           ^~~~~
y.tab.c:1215:11: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
y.tab.c:1381:14: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   *++yyvsp = yylval;
              ^~~~~~
y.tab.c:223:7: note: ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’ is implicitly deleted because the default deFinition would be ill-formed:
 union YYSTYPE
       ^~~~~~~
demo.y:48:12: error: union member ‘YYSTYPE::s’ with non-trivial ‘std::__cxx11::basic_string<_CharT,_Alloc>& std::__cxx11::basic_string<_CharT,_Alloc>::operator=(const std::__cxx11::basic_string<_CharT,_Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
     string s;
            ^
demo.y:49:16: error: union member ‘YYSTYPE::v’ with non-trivial ‘std::vector<_Tp,_Alloc>& std::vector<_Tp,_Alloc>::operator=(const std::vector<_Tp,_Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]’
     vector<int>v;
                ^
y.tab.c:1412:24: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   yyval = yyvsp[1-yylen];
                        ^
y.tab.c:1569:14: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   *++yyvsp = yyval;
              ^~~~~
y.tab.c:1713:14: error: use of deleted function ‘YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)’
   *++yyvsp = yylval;
              ^~~~~~
y.tab.c: In function ‘void __static_initialization_and_destruction_0(int,int)’:
y.tab.c:1174:9: error: use of deleted function ‘YYSTYPE::~YYSTYPE()’
 YYSTYPE yylval;
         ^~~~~~

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)