Marshall Eubanks
Marshall Eubanks

Reputation: 264

Class constructor: unresolved externals

I've been having one of those days where I keep having and endless series of inexplicable errors.

The one that's bugging me most is probably a really dumb beginner's error, but God forbid my google fu finds me the answer today.

So, I've got a very, very simple program, with three files: main.cpp, date.cpp, and date.h. It's actually from an example I found, but that's the kind of day I'm having: even sample code gives me errors.

Since this is stupid short, I'll just post the code:

main.cpp:

#include "Date.h"

int main(void) {
    Date today(2,2,2);
    return 0;
}

date.h:

#ifndef DATE_H
#define DATE_H

class Date
{
private:
    int m_nMonth;
    int m_nDay;
    int m_nYear;


public:
    Date();
    Date(int nMonth, int nDay, int nYear);

    void SetDate(int nMonth, int nDay, int nYear);

    int GetMonth() { return m_nMonth; }
    int GetDay()  { return m_nDay; }
    int GetYear() { return m_nYear; }
};

#endif

And finally date.cpp:

#include "Date.h"

// Date constructor
Date::Date() {
    SetDate(1,1,1);
}

Date::Date(int nMonth, int nDay, int nYear)
{
    SetDate(nMonth, nDay, nYear);
}

// Date member function
void Date::SetDate(int nMonth, int nDay, int nYear)
{
    m_nMonth = nMonth;
    m_nDay = nDay;
    m_nYear = nYear;
}

Upon compilation (visual C++) I get this error:

main.obj : error LNK2019: unresolved external symbol "public: __thiscall Date::Date(int,int,int)" (??0Date@@QAE@HHH@Z) referenced in function _main main.exe : fatal error LNK1120: 1 unresolved externals

Only problem is, I'm pretty sure I wrote that constructor, and I'm pretty sure I included the header file. So what I'm I missing?

Upvotes: 0

Views: 153

Answers (1)

Varo
Varo

Reputation: 851

Make your data object created before the linking process i.e the Data.cpp is attached to your project and got compiled

Upvotes: 1

Related Questions