错误 C2146 的可能原因:语法错误:缺少 ';'在标识符之前

发布于 12-10 14:40 字数 284 浏览 3 评论 0原文

我正在做一个示例应用程序,其中声明了一个结构:

 // common.h
 typedef struct MyStruct
 {
   int a;
 }

  //sample.h
  #include "common.h"
  int main()
  {
     MyStruct st;// getting error here
  }

C2146:语法错误:缺少“;”在标识符之前

可能的原因是什么?

I am doing a sample application where I have declared a struct:

 // common.h
 typedef struct MyStruct
 {
   int a;
 }

  //sample.h
  #include "common.h"
  int main()
  {
     MyStruct st;// getting error here
  }

C2146: syntax error : missing ';' before identifier

What are the possible reasons for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待我真够勒2024-12-17 14:40:37

有两件事:

首先,结构定义后缺少一个分号:

 // common.h
 typedef struct MyStruct
 {
     int a;
 };
  ^

但这仍然是错误的。您将需要修复其他错误。

其次,您应该像这样定义结构:

 // common.h
 typedef struct
 {
     int a;
 } MyStruct;

或者,您可以这样定义它:

 // common.h
 struct MyStruct
 {
     int a;
 };

Two things:

First, you're missing a semi-colon after the struct definition:

 // common.h
 typedef struct MyStruct
 {
     int a;
 };
  ^

Not that this is still wrong. You will need to fix the other error.

Second, you should define the struct like this:

 // common.h
 typedef struct
 {
     int a;
 } MyStruct;

Alternatively, you can define it like this:

 // common.h
 struct MyStruct
 {
     int a;
 };
身边2024-12-17 14:40:37

您的 "common.h" 标头未正确定义 MyStruct;它的末尾需要一个分号。

那么 typedef 就是空的;在 C++ 中,您不需要 typedef 来定义类型 MyStruct。 (在 C 中,您需要编写:

typedef struct MyStruct { int a; } MyStruct;

但 C++ 并不要求这样做 - 尽管它也不反对它。)

因此,编写以下内容就足够了:

struct MyStruct { int a; };

Your "common.h" header does not define MyStruct properly; it needs a semi-colon on the end.

Then the typedef is vacuous; in C++, you don't need the typedef to get type MyStruct defined. (In C, you'd need to write:

typedef struct MyStruct { int a; } MyStruct;

But C++ does not require that - though it does not object to it, either.)

So, it would be sufficient to write:

struct MyStruct { int a; };
月下凄凉2024-12-17 14:40:37

这几乎总是,因为此时尚未定义类型 MyStruct,要么是因为您包含了错误的标头,要么是类型规范由于某种原因失败。

如果该 typedef 与您在 common.h 中的 typedef 完全相同,则它将无法工作。它后面应该跟有别名类型和分号。或者您可能不需要 typedef,因为 C++ 允许您在源代码中将 MyStruct 引用为“正确的”类型。

像这样的东西工作得很好:

struct MyStruct { int a; };
int main() {
    MyStruct st;
    return 0;
}

或者甚至这样,显示了三种可能性:

struct MyStruct { int a; };
typedef struct MyStruct2 { int a; } xyzzy;
int main() {
    MyStruct st;
    MyStruct2 st2;
    xyzzy st3;
    return 0;
}

It's almost always because the type MyStruct isn't defined at that point, either because you're including the wrong header or the type specification fails for some reason.

If that typedef is exactly what you have in your common.h, it won't work. It should be followed by the aliasing type and a semicolon. Or perhaps you didn't want a typedef since C++ allows you to refer to MyStruct as a "proper" type in the source code.

Something like this works fine:

struct MyStruct { int a; };
int main() {
    MyStruct st;
    return 0;
}

Or even this, showing the three possibilities:

struct MyStruct { int a; };
typedef struct MyStruct2 { int a; } xyzzy;
int main() {
    MyStruct st;
    MyStruct2 st2;
    xyzzy st3;
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文