错误'struct'之前的预期','或'...'

expected ',' or '...' before 'struct' error

本文关键字:struct 错误      更新时间:2023-10-16

美好的一天,

我第一次遇到这种错误。

// 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( 错误:预期的","或"..."在"结构"之前 QDataStream &operator<<(QDataStream &out, const Item::Interface &interface(

2( other.h:34:17: 错误: 'struct'之前的预期主表达式返回<^
other.h:34:17: 错误:在"struct'other"之前预期';".h:34:17: 错误: 'struct'

之前的预期主表达式

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

#define interface struct

这使编译器看到以下内容:

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

并变得非常沮丧。

重命名参数。

我找到错误的地方是变量"接口"的名称(来自小写字母(。 将其重命名为i,例如清除代码。

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

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