我已經工作了一組代表幾何形狀的各種比特模板類的,我意識到,我希望能夠專注不同的類來處理引用和指針例如方法未定義的引用部分專業類
template<typename T>
class rect{ // as in rectangle
public:
point<T> point1, point2; // Point simply contains two instances of type T
... // Twenty functions or so follow
};
template<typename T>
class rect<point<T>>{ // trying to put a point in a point makes no sense anyway
public: // so I see this technique as valid
point<T>& point1,& point2;
... // I really don't want to redefine them if I don't have to
};
的問題,我的測試程序啓動
#include <iostream>
#include <TGeometry.hpp>
template<typename T>
class test{
public:
T A;
void myfunc(){std::cout << "from base\n";}
void otherfunc(T O);
test(const T nA) : A(nA) {}
test(){}
};
template<typename T>
void test<T>::otherfunc(T O){A += O;}
template<typename T>
class test<T&>{
public:
T& A;
void myfunc(){std::cout << "from refr\n";}
void otherfunc(T O); // Shouldn't this default to using test<T>::otherfunc?
test(T& nA) : A(nA) {}
};
int main(){ using std::cout;
using namespace d2;
test<int> Atest(5);
test<int&> Btest(Atest.A);
Atest.myfunc(), Btest.myfunc();
Btest.otherfunc(Atest.A); // test<T&>::otherfunc undefined?
Atest.otherfunc(10);
std::cin.ignore();
return 0;
}
這個程序在Mingw_w64編譯,與
C:\Users\*>g++ -o test.exe quicktest.cpp -I .\Libraries
C:\Users\THEMAG~1\AppData\Local\Temp\ccszH6xM.o:quicktest.cpp:(.text+0x42):undefined reference to `A<char, 2>::f()'
collect2.exe: error: ld returned 1 exit status
這將退出可能只是缺乏對我的部分知識,但this網站(底部,在部分專業化成員下的第一個例子)意味着你可以排除部分專業化的功能定義,只要喲你已經聲明瞭它,它將默認使用主模板的定義。
如果能夠做到這一點會救我的工作日內,我就不必重新定義我的所有類的功能。所以我的問題是,什麼是保持我的代碼編譯,並且可以/如何專門化我的類的引用(我仍然需要做指針)而不必重新定義它們的函數?這只是一個參考問題,它會改變代碼的工作方式,例如如果使用函數,主要專業化的功能只會改變參考。
錯誤消息不能與您顯示的代碼相關。代碼沒有提到名爲'A'的模板,也沒有提到名爲'f'的成員函數。 –
如果您發佈代碼生成的鏈接器錯誤可能會有幫助,因爲您發佈的代碼示例中沒有對f()的引用。 – kfsone
根據ideone(http://ideone.com/JZemgY)在MVCE錯誤是'未定義參考 '測試 :: otherfunc(INT)''? –
kfsone