2012-07-15 45 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