2011-04-09 99 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定义的东西