使用 strchr 重载>>

Using strchr to overload >>

本文关键字:gt 重载 strchr 使用      更新时间:2023-10-16

我正在尝试重载>>运算符以读取单个(使用 enum Symbol {e,a,b,c,d}; 创建)符号:

istream & operator >> (istream & is, Symbol & sym) {
  Symbol Arr[]={e,a,b,c,d};
  char ch;
  is>>ch;
  if (strchr("eabcd",ch))
    sym=Arr[ch-'e'];
      else {
        is.unget(); 
        is.setstate(ios::failbit);
      }
  return is;
}

但是这读取了一些垃圾(数字)而不是我正在寻找的内容,导致在尝试使用<<过载打印它时出现分段错误,我做错了什么?编辑:哦,当然,我确实在开始时添加了using namespace std;,包括iostreamcstring相同。

这里有一些问题。首先,让我们修复您的支撑。只是总是使用牙套。很难看出什么与什么对齐:

istream & operator >> (istream & is, Symbol & sym) {
    Symbol Arr[]={e,a,b,c,d};
    char ch;
    is>>ch;
    if (strchr("eabcd",ch)) {
        sym=Arr[ch-'e'];
    }
    else {
        is.unget(); 
        is.setstate(ios::failbit);
    }
    return is;
}

好的,太好了。现在,如果用户输入类似 'a' .strchr成功,然后你做sym = Arr[ch - 'e'].但是在这种情况下ch - 'e'-4.这是某个地方完全随机的内存,所以你得到垃圾。要实际使用 strchr ,您需要执行以下操作:

const char* options = "eabcd";
if (const char* p = strchr(options, ch)) {
    sym = Arr[p - options];
}

但这有点可怕。我建议只使用开关:

switch (ch) {
    case 'e': sym = e; break;
    case 'a': sym = a; break;
    ...
    default:
        is.unget();
        is.setstate(ios::failbit);
}

此外is >> ch可能会失败,而您没有检查它。您应该:

istream& operator>>(istream& is, Symbol& sym) {
    char ch;
    if (is >> ch) {
        switch(ch) { ... }
    }
    return is;
}

如果ch'a'ch - 'e'(97 - 101)将是负数(-4),这将导致访问数组Arr越界。这会导致未定义的行为。

使用符号的方式,您需要使用 switch 语句:

switch (ch)
{
   case 'a':
      sym = a;
      break;
   case 'b':
      sym = b;
      break;
   case 'c':
      sym = c;
      break;
   case 'd':
      sym = d;
      break;
   case 'e':
      sym = e;
      break;
   default:
     // Nothing to do
     break;
}

如果要使用 Arr ,则需要将Arr定义为:

 Symbol Arr[]={a,b,c,d,e};

然后,您可以按如下方式访问数组并避免使用 switch 语句:

sym=Arr[ch-'a'];  // ch - 'a' is 0 when ch is 'a'
                  // ch - 'a' is 4 when ch is 'e'.