2011-04-09 98 views
0

請看看我的代碼。我在Visual Studio中得到的錯誤是簡單的程序不會編譯

error LNK2019: unresolved external symbol "public: int __thiscall Employee::getId(void)" ([email protected]@@QAEHXZ) referenced in function _main 

Employee.h

#ifndef EMPLOYEES 
#define EMPLOYEES 

#include <string> 
#include <iostream> 
using namespace std; 

class Employee 
{ 
private: 
    string name; 
    int id; 
public: 
    Employee(); 
    Employee(string nm, int idd); 
    string getName(); 
    int getId(); 
}; 

#endif 

Employee.cpp

#include "Employee.h" 
#include <string> 

Employee::Employee() 
{ 
    name="unknown"; 
    id=0; 
} 

Employee::Employee(string nm, int idd) 
{ 
    name=nm; 
    id=idd; 
} 

string Employee::getName() 
{ 
    return name; 
} 

int Employee::getId() 
{ 
    return id; 
} 

司機

#include <iostream> 
#include <string> 
#include "Employee.h" 
using namespace std; 

int main() 
{ 
    Employee bob("bob", 3); 
    cout << bob.getId(); 
} 
+3

你最好不要使用「using namespace std;」在標題(不涉及你的問題)。 – ysdx 2011-04-09 23:58:27

+1

編譯和鏈接行怎麼樣? – 2011-04-09 23:58:40

+0

你如何編譯你的程序? – Muggen 2011-04-09 23:59:04

回答

2

看起來你忘了這兩個文件鏈接在一起。確保你將Employee.cpp和main.cpp文件連接在一起。他們是否添加到同一個VS項目?

編輯:退房this link。它已經過時了,但看起來像你做不是使項目包含所有的文件。他們應該自動鏈接。

1

看起來你是不是鏈接員工.o,Employee.cpp的結果,或直接添加Employee.cpp與main.cpp中

編譯最後連接器看到主,但無法找到Employee.cpp定義的東西