2015-11-01 71 views
0

我想實現工廠類和接口。但我收到以下錯誤消息。我創建了一個工廠類,它決定返回NormalTaxManager或ImportedTaxManager的類。我使用接口提供了抽象。如何實現工廠模式?

#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); 
} 

問題:

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

回答

1

您需要TaxInterface之前聲明NormalTaxManagerImportedTaxManager

而且你也需要做相反的事情。

爲了解決這個問題(經典)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

問候

+0

這個問題沒有回答。 – skypjack

+0

它爲我工作! – hmims

+0

@skypjack其實我從閱讀中瞭解到,hmims有一種非常好的Factory,顯示的錯誤代碼與循環引用有關。我不是關於工廠模式的一般問題,我認爲hmims同意我的觀點。 –

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; 
}