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文件,但是模板的定義必須保存在頭文件中,這樣可能會崩潰。
在此先感謝
感謝您的回答,但不幸的是,這些選項都不起作用,不斷得到相同的錯誤。 – Adam 2014-10-30 13:05:16
@Adam您必須更改這兩個類模板。查看我的編輯。 – Columbo 2014-10-30 13:11:17
相應地改變了,甚至嘗試了幾種組合,仍然是相同的錯誤。 – Adam 2014-10-30 13:23:37