我在模板和組合風格編碼方面遇到了一些麻煩。我有一個對象在* this參數的另一個構造函數中創建。對不起,如果我不清楚。代碼如下:使用用戶定義的類作爲參數的模板
在outer.h文件:
class outer {
public:
outer(int w, int l);
int getWidth();
int getLength();
private:
inner<outer> test(*this);
int width;
int length;
};
outer::outer(int w, int l) {
width = w;
length = l;
}
int outer::getLength() {
return length;
}
在inner.h文件
template<typename T>
class inner {
public:
inner(T &name);
private:
int top;
int bot;
};
template<typename T>
inner<T>::inner(T &name) {
top = name.getLength() /2;
bot = -name.getLength()/2;
}
我不知道這是允許的,因爲我可以」在網上找到解決這個問題的東西。編譯器在outer.h中的這個語句有問題。
在此先感謝您的幫助。
確保您使用C++ 11進行編譯,並且您的編譯器支持該功能。 – chris 2013-03-19 00:01:27
@chris只能用'='或'{}',而不能用'()'。 – juanchopanza 2013-03-19 00:06:35
@ juanchopanza,哦,是的,沒錯。無論如何,無論如何我都會使用{}。 – chris 2013-03-19 00:37:24