0

I am trying to implement factory class and interface. But i am getting the below error message. I have created a factory class which decides which class to return NormalTaxManager or ImportedTaxManager. I have provided the abstraction using interface.

#include<iostream>
#include<vector>
using namespace std;

class TaxInterface
{
public:
    virtual int calculate_tax(int price,int quantity)=0;
};

class TaxFactory
{
public:
    // Factory Method
    static TaxInterface *callManager(int imported)
    {
        if (imported == 0)
            return new NormalTaxManager;
        else
            return new ImportedTaxManager;
    }
};

class NormalTaxManager: public TaxInterface
{
public:
    virtual int calculate_tax(int price,int quantity)
    {
        cout << "NormalTaxManager\n";
        price=quantity*price*10/100;
        return price;
    }
};

class ImportedTaxManager: public TaxInterface
{
public:
    virtual int calculate_tax(int price,int quantity)
    {
        cout << "ImportedTaxManager\n";
        price=quantity*price*5/100;
        return price;
    }
};

int main()
{
    TaxFactory f;
    TaxInterface *a = f.callManager(1);
    a->calculate_tax(100,2);
    //    int price=TaxInterface::callManager(1)->calculate_tax(100,2);
}

Problem:

 error: ‘NormalTaxManager’ does not name a type
 error: ‘ImportedTaxManager’ does not name a type
4

2 回答 2

1

你需要声明NormalTaxManagerandImportedTaxManager之前TaxInterface

你还需要做相反的事情。

为了解决(经典)C++ 循环引用问题,您需要在 .cpp 和 .h 文件之间拆分代码

TaxInterface它放在一个自己的文件中是抽象的并且没有实现:TaxInterface.h。

例如,分成ImportedTaxManager两个文件:

。H

#pragma once
#include "taxinterface.h"

class ImportedTaxManager : public TaxInterface
{
public:
    virtual int calculate_tax(int price, int quantity);
};

.cpp

#include "stdafx.h"
#include<iostream>
using namespace std; 

#include "ImportedTaxManager.h"

int ImportedTaxManager::calculate_tax(int price, int quantity)
{
    cout << "ImportedTaxManager\n";
    price = quantity*price * 5 / 100;
    return price;
}

如果你“聪明”,你可以“保存”一些文件。

但是维护在标头 (.h) 和实现 (.cpp) 之间拆分的代码更容易。

因为 C++ 需要声明所使用的所有内容,所以您经常会得到循环引用,可以通过拆分 .h 和 .cpp 来解决

完整的工作解决方案:http: //1drv.ms/1Pe25SQ

问候

于 2015-11-01T14:10:47.440 回答
0

您只需要在 TaxFactory 类上方声明从 TaxInterface 派生的 2 个类,以便它们对其可见。我还在 main() 中提出了一些建议......

#include<iostream>
#include<vector>
using namespace std;

class TaxInterface
{
public:
    virtual int calculate_tax(int price, int quantity) = 0;
};

class NormalTaxManager : public TaxInterface
{
public:
    virtual int calculate_tax(int price, int quantity)
    {
        cout << "NormalTaxManager\n";
        price = quantity*price * 10 / 100;
        return price;
    }
};

class ImportedTaxManager : public TaxInterface
{
public:
    virtual int calculate_tax(int price, int quantity)
    {
        cout << "ImportedTaxManager\n";
        price = quantity*price * 5 / 100;
        return price;
    }
};

class TaxFactory
{
public:
    // Factory Method
    static TaxInterface *callManager(int imported)
    {
        if (imported == 0)
            return new NormalTaxManager;
        else
            return new ImportedTaxManager;
    }
};

int main()
{
    vector<TaxInterface*> interfaces;
    vector<int> taxTypes = { 0, 0, 1, 0, 1, 1, 1, 0, 1 };

    for (vector<int>::iterator i = taxTypes.begin(); i != taxTypes.end(); i++) {
        interfaces.push_back(TaxFactory::callManager(*i));
    }

    for (int i = 0; i < interfaces.size(); i++) {
        interfaces[i]->calculate_tax(100, 2);
    }   

    // system("pause");
    return 0;
}
于 2015-11-01T14:29:05.113 回答