2013-06-01 81 views
3

我開始自學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; 
} 
+2

你還沒有提到錯誤。而且,絕對不需要在C++中使用'typedef struct {} blah;'。只需使用'struct blah {};'。 C++提供比普通C結構更多的功能,例如可用於此的構造函數,以及運算符重載,這對於打印它很有用,但後者對於您現在需要的內容可能稍微先進一些。 – chris

+2

除了缺少分號*包含*'EmployeeT.h'和'Employee.h'將是不同的文件。 – Joe

+0

您是否收到任何錯誤?新聞告訴我們他們是什麼。 – 0x499602D2

回答

4

在主你不希望#include "EmployeeT.h"而不是#include <Employee.h>

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