2012-07-15 43 views
3

我想創建一個表示多項式的LinkedList的實現。鏈接列表將成爲「期限」列表。一個術語是Data的一個實現(它是一個帶有方法的抽象類:compareTo()和toString())。 Polynomial類有一個名爲head的變量,我試圖初始化爲Term。我的編譯器說我「不能聲明抽象類型的成員:Term」,但我不認爲Term是抽象的,因爲它是Data(抽象類)的實現。如果你們可以看看這個,讓我知道我失蹤的任何巨大的紅旗,我將不勝感激。 Collection.h:這個班爲什麼抽象?

class Data { 
    public: 
    virtual ~Data() {} 

virtual int compareTo(Data * other) const = 0; 

virtual string toString() const = 0; 
}; 

class Term : public Data { 
public: 
int coefficient; 
string variable1; 
int exponentX; 
string variable2; 
int exponentY; 
Term * next; 

Term(int coeff, string var1, int exp1, string var2, int exp2, Term * next) : 
    coefficient(coeff), 
    variable1(var1), 
    exponentX(exp1), 
    variable2(var2), 
    exponentY(exp2), 
    next(next) {}; 

string convertInt(int number) { 
    stringstream ss;//create a stringstream 
    ss << number;//add number to the stream 
    return ss.str();//return a string with the contents of the stream 
} 

int compareTo(Term * term) { 
    if(this->exponentX > term->exponentX) { 
    return 1; 
    } 
    else if(this->exponentX < term->exponentX) { 
    return -1; 
    } 
    else { 
     if(this->exponentY > term->exponentY) { 
     return 1; 
     } 
     else if(this->exponentY < term->exponentY) { 
     return - 1; 
     } 
     else { 
     return 0; 
     } 
    } 
} 
string toString() { 
    stringstream s; 
    int * current = &this->coefficient; 
    if(*current == 1 || *current == -1) { 
    } 
    else if(coefficient != 0) { 
    s << convertInt(coefficient); 
    } 
    else { return s.str(); } 
    if(variable1 != "" && this->exponentX != 0) { 
    s << variable1; 
    s << convertInt(exponentX); 
    } 
    if(variable2 != "" && this->exponentY != 0) { 
    s << variable2; 
    s << convertInt(exponentY); 
    } 
return s.str(); 
} 
}; 

此外,這裏是LinkedList的實現。那裏還有其他一些方法,但他們似乎沒有提出任何問題。

LinkedList.cpp:

class Polynomial : public LinkedList { 
public: 
Term head; 

Polynomial() { 
this->head = NULL; 
} 

~Polynomial() { 
Term * current = head; 
    while (current != NULL) { 
     Term * next = current->next; 
     delete current; 
     current = next; 
    } 
} 

謝謝!

回答

6

當您重寫虛擬方法時,必須匹配的函數簽名。返回類型可能會根據協方差規則變化,但參數類型必須完全相同。

在基礎類Data功能compareTo被聲明爲

virtual int compareTo(Data * other) const 

在派生Term類它被聲明爲

int compareTo(Term * term) 

首先,參數類型是不同的。其次,const丟失。

這意味着你在派生類中編寫了一個完全不相關的函數。它不覆蓋基類的純虛函數。由於基本純虛函數保持不被覆蓋,類Term仍然是抽象的。

Term必須聲明你的函數正是由於

int compareTo(Data * other) const 

我假設你希望使用TermcompareToTerm至 - Term比較。但在此設計中,您必須將Data作爲參數,然後將其轉換爲Term,或者使用雙重調度技術。

P.S.最重要的是你聲明Term對象爲您Polynomial類的成員head再後來使用它,就好像它是一個指針

Term * current = head; 

這是沒有意義的。如果你想讓你的head成爲一個指針,將它聲明爲一個指針。如果你希望它是一個對象,那麼停止使用它作爲指針。無論是這個還是那個。

+0

值得一提的是[C++ 11'override' identifier](http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final)。 – 2012-07-15 22:41:46

+0

@AndreyT謝謝你的幫助!我會喜歡,但我還沒有聲譽。我不明白爲什麼指針會這樣做。僅此一項就消除了我得到的大量錯誤。 – user1527482 2012-07-16 02:14:06

+0

@ user1527482:將頭部聲明轉換爲指針也會隱藏原始錯誤(關於抽象類)。不過,它會在其他地方重演。 – AnT 2012-07-16 02:48:55