C ++虚函数解析问题

时间:2015-11-03 06:22:17

标签: c++ function virtual

程序在此函数调用解析时抛出错误:

for(vector<Catalog*>::iterator i=m_subDeptList.begin(); i!=m_subDeptList.end();  ++i)
         (*i)->displayDiscription();

错误:

test.cpp: In member function ‘void Department::displayDiscription()’:
test.cpp:73:14: error: ‘class Catalog’ has no member named ‘displayDiscription’
    (*i)->displayDiscription();

但以下呼叫解决方案没有问题:

for(vector<Product*>::iterator i=m_products.begin(); i!=m_products.end();  ++i)
        (*i)->displayDiscription();
for(vector<Catalog*>::iterator i=m_subDeptList.begin() ; i!=m_subDeptList.end();  ++i)
        (*i)->addDiscount(discount);
for(vector<Product*>::iterator  i=m_products.begin(); i!=m_products.end();  ++i)
        (*i)->addDiscount(discount);

有人可以解释一下吗?

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class Catalog
{
    string m_name;
public:
    Catalog(const string &name):m_name(name){}
    //virtual ~Catalog(){}
    const string& getName()
    {
        return m_name;
    }

    virtual bool addDiscount(unsigned int discount)=0;
    virtual void displayDescription()=0;      
};
class Product:public Catalog
{
    unsigned short m_discount; // discount in %
    long double m_price;
public:
    Product(const string& productName, const long double &price):Catalog(productName),m_price(price){}
    void displayDiscription()
    {
        cout<<getName()<<"price: "<<getPrice()<<endl;
    }    
    bool addDiscount(const unsigned int discount)
    {
        if( discount > 100 || discount < 0)
            return false;
        m_discount+=discount;
        return true;
    }
    long double getPrice()
    {
        return m_price;
    }

};
class Department:public Catalog
{
    vector<Product*> m_products;
    vector<Catalog*> m_subDeptList;
    unsigned int discountApplied;
public:
    Department(const string &name):Catalog(name){}

    bool addDiscount(unsigned int discount) // add discount to all products in this dept/subDept
    {
        if( discount > 100 || discount < 0)
            return false;

        for(vector<Catalog*>::iterator i=m_subDeptList.begin() ; i!=m_subDeptList.end();  ++i)
            (*i)->addDiscount(discount);
        for(vector<Product*>::iterator  i=m_products.begin(); i!=m_products.end();  ++i)
            (*i)->addDiscount(discount);
        discountApplied+=discount;
        return true;
    }    

    void displayDiscription()
    {

        cout<<getName()<<endl;

        if(!m_subDeptList.empty() )
        for(vector<Catalog*>::iterator i=m_subDeptList.begin(); i!=m_subDeptList.end();  ++i)
             (*i)->displayDiscription();

        if(!m_products.empty() )
            for(vector<Product*>::iterator i=m_products.begin(); i!=m_products.end();  ++i)
            (*i)->displayDiscription();

        return;

    }  
/*
    void addProduct(Product* pProduct)
    {
        m_products.push_back(pProduct);

    }
    void addDepartment(Catalog* pDepartment)
    {
        m_subDeptList.push_back(pDepartment);

    }
*/
};

int main(int argc, char** argv) {
/*    
    Department megaStoreCatalog("catalog");
    Department stationary("Stationary");
    Product pen("pen", 5);
    stationary.addProduct(&pen);
    Department stationaryLiquid("Stationary-Liquid");
    Product ink("Bril",30);
    stationaryLiquid.addProduct(&ink);
    megaStoreCatalog.addDepartment(&stationary);
    stationary.addDepartment(&stationaryLiquid);

    megaStoreCatalog.addDiscount(10);

  */  
    return 0;
}

2 个答案:

答案 0 :(得分:2)

您不能只在const中添加addDiscount。它改变了函数签名。

您应该使用C ++ 11 override keyword

答案 1 :(得分:1)

无法解析,因为在纯虚函数声明的基类签名中找不到两个派生类方法。您的问题不是实现问题,而是拼写错误。

查看Base和Derived类中的拼写;你拼写为discription,另一拼为description。您还有一个纯虚方法,其中const未在基类中以这种方式定义。另一件事是你的构造函数只有1个参数,所以你应该在它的名字之前声明带有explicit关键字的构造函数。您不应该在基础中使用默认构造函数,即使它是空的或者没有做任何事情并将其声明为虚拟构造函数。

在您的基础课

  

virtual void displayDescription()= 0;

在您的派生类中

void displayDiscription()
{
    cout<<getName()<<"price: "<<getPrice()<<endl;
}    

&安培;

void displayDiscription()
{

    cout<<getName()<<endl;

    if(!m_subDeptList.empty() )
    for(vector<Catalog*>::iterator i=m_subDeptList.begin(); i!=m_subDeptList.end();  ++i)
         (*i)->displayDiscription();

    if(!m_products.empty() )
        for(vector<Product*>::iterator i=m_products.begin(); i!=m_products.end();  ++i)
        (*i)->displayDiscription();

    return;

}