Robert Dennis
Robert Dennis

Reputation: 345

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("bob", 3);
    cout << bob.getId();
}

Upvotes: 0

Views: 163

Answers (2)

evgeny
evgeny

Reputation: 2584

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.

Upvotes: 2

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23198

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

Upvotes: 1

Related Questions