我开始自学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;
}
答案 0 :(得分:9)
你错过了分号:
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以来引入的uniform initialization
。
答案 1 :(得分:4)
主要不是#include "EmployeeT.h"
而不是#include <Employee.h>
?