加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 结构错误

发布时间:2020-12-16 10:33:38 所属栏目:百科 来源:网络整理
导读:我开始自学C并且遇到了一个错误,我认为这个错误非常简单,但没有抓住它.我创建了以下名为EmployeeT.h的头文件 #ifndef EMPLOYEET_H_INCLUDED#define EMPLOYEET_H_INCLUDEDtypedef struct{ char firstInitial; char middleInitial; char lastInitial; int empl
我开始自学C并且遇到了一个错误,我认为这个错误非常简单,但没有抓住它.我创建了以下名为EmployeeT.h的头文件

#ifndef EMPLOYEET_H_INCLUDED
#define EMPLOYEET_H_INCLUDED

typedef struct
{
    char firstInitial;
    char middleInitial;
    char lastInitial;
    int employeeNumber;
    int salary;
} EmployeeT

#endif // EMPLOYEET_H_INCLUDED

以主为

#include <iostream>
#inclide <Employee.h>

using namespace std;

int main()
{
    EmployeeT anEmployee;
    anEmployee.firstInitial = 'M';
    anEmployee.middleInitial = 'R';
    anEmployee.lastInitial = 'G';
    anEmployee.employeeNumber = 42;
    anEmployee.salary = 80000;
    cout << "Employee: " << anEmployee.firstInitial <<
                            anEmployee.middleInitial <<
                            anEmployee.lastInitial << endl;
    cout << "Number: " << anEmployee.employeeNumber << endl;
    cout << "Salary: " << anEmployee.salary <<endl;

    return 0;
}

解决方法

你错过了分号:

typedef struct
 {
     char firstInitial;
     char middleInitial;
     char lastInitial;
     int employeeNumber;
     int salary;
 } EmployeeT;
           //^^Must not miss this ;

与此同时:

#inclide <Employee.h>
    //^^typo

应该:

#include "Employee.h"

最后一点:您可以按如下方式初始化结构:

anEmployee = {'M','R','G',42,80000}; 
             //It will assign values to field in automatic way

如果你很好奇,你也可以看看自C 11以来引入的统一初始化.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读