2014-10-30 71 views
1

我已經看到許多與此問題相關的問題,但仔細聽從成員的建議後,我的問題仍然存在。代碼很簡單。我只有下面的頭文件(「instrument.h」),其中包含了基類和模板類:C++中的模板繼承和Xcode中未定義的符號

#include <stdio.h> 
#include <string> 

using namespace std; 

class Instrument 
{ 
public: 
    Instrument(); 
    virtual void print() const = 0; 
}; 

template <class parameter> class Equity : public Instrument 
{ 
public: 
    Equity(); 
    virtual void print() const; 
}; 

現在,在我的main.cpp中的主要功能,我只做到以下幾點:

#include "instrument.h" 
#include <iostream> 

int main() { 

    Equity<double> pb;   
    return 0; 
} 

好了,我得到了非常著名的錯誤:

Undefined symbols for architecture x86_64: 
    "Equity<double>::Equity()", referenced from: 
     _main in main.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我已經在構建設置已經改變了C++標準庫與libstdC++,也爲默認的編譯器,等等。我的項目設置有問題嗎?模板可能被錯誤地實現了嗎?我在想我也應該有一個instrument.cpp文件,但是模板的定義必須保存在頭文件中,這樣可能會崩潰。

在此先感謝

回答

1

您宣佈兩個InstrumentEquity默認的構造函數,但無處定義它們。

適當地改變它們的定義:

public: 
    Equity() = default; // Or {} in pre-C++11 
//   ^^^^^^^^^ 

(和等效爲Instrument

您也可以完全省略任何默認構造函數的聲明,現在因爲你沒有申報任何兩個其他的構造EquityInstrument,默認構造函數將自動生成。

+0

感謝您的回答,但不幸的是,這些選項都不起作用,不斷得到相同的錯誤。 – Adam 2014-10-30 13:05:16

+0

@Adam您必須更改這兩個類模板。查看我的編輯。 – Columbo 2014-10-30 13:11:17

+0

相應地改變了,甚至嘗試了幾種組合,仍然是相同的錯誤。 – Adam 2014-10-30 13:23:37