开发者

simple program wont compile

Please take a look at my code. The error I get in Visual Studio is

error LNK2019: unresolved external symbol "public: int __thiscall Employee::getId(void)" (?getId@Employee@@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;
}

driver

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

int main()
{   
    Employee bob("开发者_Go百科bob", 3);
    cout << bob.getId();
}


Looks like you forgot to link the two files together. Make sure that you're linking Employee.cpp and main.cpp files together. Are they added to the same VS project?

EDIT: check out this link. It's outdated, but looks like you did not make a project that contains all the files. They should be linked automagically.


Looks like you are not linking Employee.o, result of Employee.cpp, or directly adding Employee.cpp to compile with main.cpp

The final linker sees the main but can't find the things defined in Employee.cpp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜