【问题标题】:struct declaration syntax error [closed]结构声明语法错误
【发布时间】:2010-08-01 19:10:30
【问题描述】:

我有以下代码:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}

但是有错误:

1>------ Build started: Project: array_structures1, Configuration: Debug Win32 ------
1>Build started 8/1/2010 9:26:47 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\array_structures1.unsuccessfulbuild".
1>ClCompile:
1>  array_structures.cpp
1>c:\users\david\documents\visual studio 2010\projects\array_structures1\array_structures1\array_structures.cpp(9): error C2236: unexpected 'struct' 'stud'. Did you forget a ';'?
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.84
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

请帮忙。如何修复这些错误?

【问题讨论】:

  • 我开始认为你的所有问题都应该关闭,因为太本地化了。
  • @Pavel 所有这些人的帖子都应该被关闭为“自私和懒惰”,如果有这样一个密切的原因。
  • @Neil,是的,这就是我使用“过于本地化”的原因。
  • 伙计们,有人帮助我,但有人试图降低我的声誉,为什么我不明白这是个问题,我已经发布了什么问题?我见过很多人的问题,其中一些很简单,但我从来没有减少过,为什么是我?
  • @davit - 我没有投反对票,但我想原因是错误消息会告诉您确切的问题,如果您费心阅读它,您不会需要问这个问题。我什至不会 C++,我可以在大约 5 秒内找出问题所在。

标签: c++ syntax-error


【解决方案1】:

请阅读错误信息:

错误 C2236:意外的“结构”“螺柱”。你忘了一个';'吗?

struct stud 声明末尾缺少分号。

【解决方案2】:

我不懂 C++,但也许:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }; // notice the semicolon
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}

【讨论】:

    【解决方案3】:

    “你忘了一个';'吗?”说明一切;

    在你声明一个结构后,你必须加上“;”。 你也可以这样做:

    struct struct_name {
     struct_variables; } new_str;
    

    这将创建结构并创建该结构类型的新变量。

    所以,你可以很容易地做到这一点:

    struct  stud{
    
        int roll;
        char name[10];
        double marks;
        } stud1={1,"ABC",99.9}, stud2={2,"xyz",80.0};
    

    而且,在你创建一个结构之后,要声明一个该结构类型的变量,你只需要编写


    “stud stud1”而不是“struct stud stud1”

    【讨论】:

      【解决方案4】:

      您的代码在 g++ 下完美编译。

      但你可以试试这个:

      struct stud {
      
          int roll;
          char name[10];
          double marks;
      };
      

      【讨论】:

      • 使用 > 编译的代码不会,也从来不会意味着代码符合标准。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多