“(”标记之前出现意外的主表达式

Unexpected primary-expression before ‘(’ token

本文关键字:意外 表达式      更新时间:2023-10-16

我搜索了这个问题,但无法弄清楚如何解决这个问题:

class DtEffect;
template <typename VertexFormat> 
class DtEffectRenderer : public DtFormatRenderer<VertexFormat>
{
public:
template <typename MemberType>
static DtEffect::VertexAttribPtrInfo VertexAttrib(const MemberType VertexFormat::* member)
{
    return DtEffect::VertexAttribPtrInfo(
        reinterpret_cast<const GLvoid*>(offsetof(VertexFormat, *member))
        , DtAttributeType<MemberType>::value
        , DtAttributeType<MemberType>::size);
}
protected:
   DtEffect* myEffect;
};

错误消息:

../../include/vrvGraphics/DtEffectRenderer.h: In static member function ‘static makVrv::DtEffect::VertexAttribPtrInfo makVrv::DtEffectRenderer<VertexFormat>::VertexAttrib(const MemberType VertexFormat::*)’:
../../include/vrvGraphics/DtEffectRenderer.h:115: error: expected primary-expression before ‘(’ token
../../include/vrvGraphics/DtEffectRenderer.h:116: error: expected unqualified-id before ‘*’ token
../../include/vrvGraphics/DtEffectRenderer.h:116: error: expected ‘)’ before ‘*’ token

有什么想法吗?

您似乎正在尝试使用offsetof宏来获取通过指向成员的指针标识的成员的偏移量:

offsetof(VertexFormat, *member)

这是行不通的,因为offsetof宏的第二个参数必须是成员的名称,而不是可用于访问成员的任何类型的表达式。编译错误显然是神秘的,但编译器几乎无能为力,因为offsetof是一个宏。

有关使用指向成员的指针查找成员偏移量的信息,请参阅 0xbadf00d 对此问答的回答。他的方法密切复制了offsetof宏的内部工作原理,但他使用指向成员的指针而不是成员的名称。

看起来你在VertexAttribPtrInfo之后缺少一个'('。我在下面重新添加了它,尝试一下看看它是否有效。

template <typename MemberType>
static DtEffect::VertexAttribPtrInfo VertexAttrib(const MemberType VertexFormat::* member)
{
return DtEffect::VertexAttribPtrInfo((
    reinterpret_cast<const GLvoid*>(offsetof(VertexFormat, *member))
    , DtAttributeType<MemberType>::value
    , DtAttributeType<MemberType>::size);
}