Hjg*_*jgb 3 c++ codeblocks header-files
我开始自学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
Run Code Online (Sandbox Code Playgroud)
以主为
#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;
}
Run Code Online (Sandbox Code Playgroud)
你错过了分号:
typedef struct
{
char firstInitial;
char middleInitial;
char lastInitial;
int employeeNumber;
int salary;
} EmployeeT;
//^^Must not miss this ;
Run Code Online (Sandbox Code Playgroud)
与此同时:
#inclide <Employee.h>
//^^typo
Run Code Online (Sandbox Code Playgroud)
应该:
#include "Employee.h"
Run Code Online (Sandbox Code Playgroud)
最后一点:您可以按如下方式初始化结构:
anEmployee = {'M','R','G',42, 80000};
//It will assign values to field in automatic way
Run Code Online (Sandbox Code Playgroud)
如果你很好奇,你也可以看看uniform initialization自C++ 11以来引入的内容.