簡體   English   中英

類構造函數:未解析的外部

[英]Class constructor: unresolved externals

在那段日子里,我一直經歷着無休止的一系列莫名其妙的錯誤。

最困擾我的人可能是一個非常愚蠢的初學者的錯誤,但是上帝禁止我的Google Fu今天找到了我的答案。

因此,我有一個非常非常簡單的程序,其中包含三個文件:main.cpp,date.cpp和date.h。 它實際上是從我發現的一個示例中得出的,但這就是我所經歷的一天:即使示例代碼也給我帶來了錯誤。

由於這很愚蠢,所以我將發布代碼:

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

最后是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;
}

編譯(Visual C ++)后,出現此錯誤:

main.obj:錯誤LNK2019:無法解析的外部符號“ public:__thiscall Date :: Date(int,int,int)”(?? 0Date @@ QAE @ HHH @ Z)在函數_main main.exe中引用:致命錯誤LNK1120: 1個未解決的外部

唯一的問題是,我確定我編寫了該構造函數,並且確定包含了頭文件。 那我想念的是什么?

在鏈接過程之前創建您的數據對象,即將Data.cpp附加到您的項目並進行編譯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM