【问题标题】:expected ',' or '...' before 'struct' error'struct' 错误之前的预期 ',' 或 '...'
【发布时间】:2018-05-14 08:59:54
【问题描述】:

早安,

我第一次遇到这种错误。

// another.h
struct Item {
  QString name = QString();
  QString description = QString();
  QVariant value = QVariant();
  struct Interface {
    uint id = 0;
    uint pos = 0;
  } mkio, uks;
  struct Element {
    float weight = 0;
    struct Range {
      uint from = 0;
      uint to = 0;
    } range;
  int index = 0;
} element;
};

我想将此嵌套结构存储在流中。所以,

// another.h
QDataStream &operator<<(QDataStream &out, const Item::Interface &interface)
{
  return out << interface.id
             << interface.pos;
}
// and another overload operator>> and operator<<...
// Another fields of the `Item` struct are compiling without any error.

1) 错误:在 'struct' 之前应有 ',' 或 '...' QDataStream &operator

2) another.h:34:17: error: 'struct' 之前的预期主表达式
返回 ^
另一个.h:34:17:错误:预期的';'在“结构”之前
another.h:34:17: error: 'struct' 之前的预期主表达式

【问题讨论】:

  • 您能提供一个SSCCE 吗?看来您的申报顺序有些问题。或者您忘记包含 QDataStream 标头。顺便说一句,你的代码不可读,尽量避免这种嵌套的结构声明。
  • 你能在发生错误时标记行吗
  • }; - 最后一行 - 它是什么?
  • 致 Dmitry:首先定义结构然后重载运算符。我包括 QDataStream。重新排序结构并定义Interface 没有结果...

标签: c++ qt c++11 struct


【解决方案1】:

在代码的某个地方,很可能在库头中,隐藏了以下(或与之非常相似的):

#define interface struct

这会让编译器看到这个:

QDataStream &operator<<(QDataStream &out, const Item::Interface &struct)
{
  return out << struct.id
             << struct.pos;
}

然后变得非常沮丧。

重命名参数。

【讨论】:

    【解决方案2】:

    我发现错误的地方是变量“interface”的名称(来自小写字母)。 将其重命名为i,例如清除代码。

    QDataStream &operator<<(QDataStream &out, const Item::Interface &i /*nterface*/)
    

    在命名空间中声明的类型 Interface 因此更改其名称没有结果。

    谢谢大家??

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多