2017-02-24 69 views
1

不同的值,試圖找到一個辦法讓我的動態轉換工作,但我不斷收到運行時錯誤。它在打印出來時跳轉到else語句塊的值(當它應該是一個if塊的值,但是當我在派生類中調用它時甚至沒有被使用,所以它顯示錯誤的值並且根本不會在。計算這是爲什麼感謝您的幫助使用動態投地返回變量中派生類

class Package 
{ 
protected: 
    string name_and_address = "?"; 

    double cost = 0.0; 
    double discount = 0.0; 
    double discount_rate = 0.0; 

    bool overnight_delivery = false; 
    bool insured = false; 

    string package_contents = "?"; 

    double shipcost = 0.0; 

public: 
    Package() {}; 
    ~Package() {}; 

protected: 
    virtual double calculate_cost() = 0; 

    class Video_Games {}; //defined the classes 
    class Genius_Phone {}; 
    class Sausage {}; 
    class Albums {}; 

    // here I'm trying to change "shipcost" inside each of the derived classes 
    // to their respective and appropriate dollar values. 
    // However, I am getting a runtime error wherein it will jump DIRECTLY 
    // to the else statement 50.00 value for Video_Games 
    // and not even calculate the value as 50.00. SO it's doubly useless. 
    // It just skips to the else value and doesn't even factor 
    // that into the Video_Games calculation when I try to test it. 
    virtual double shipping_cost() { 
     if (dynamic_cast<Video_Games*>(this)) 
      return 4.99; 
     else if (dynamic_cast<Genius_Phone*>(this)) 
      return 25.00; 
     else if (dynamic_cast<Sausage*>(this)) 
      return 9.00; 
     else 
     { 
      // should be assigned to class Albums, 
      // but is somehow being triggered by class Video_Games. 
      // Not sure why this is. 
      return 50.00; 
     } 
    } 
}; 

class Video_Games :public Package 
{ 
private: 
    int num_games = 0; 

public: 
    Video_Games(string location, int number_of_games, bool express, bool insurance) 
    { 
     num_games = number_of_games; 
     name_and_address = location; 
     overnight_delivery = express; 
     insured = insurance; 

     package_contents = to_string(num_games) + " Video Game(s)"; 

     cost = calculate_cost(); 
     discount = calculate_discount(); 
     shipcost = shipping_cost(); 
    } 

    ~Video_Games() {}; 

protected: 

    double calculate_cost() 
    { 
     cost = num_games * 19.99; 
     // this is where the magic should happen. 
     // shipcost here should be 4.99, but instead it's not even being used here. 
     // In fact - it's empty. I'm not sure why shipcost 
     // when set equal shipping_cost() is not returning that appropriate value. 
     // Very baffling. 
     if (overnight_delivery) { cost += shipcost; } 
     if (insured) { cost *= 1.06; } 

     return cost; 
    } 
}; 
+0

那是因爲你做錯了。你應該看看這個頁面如何使用它:http://en.cppreference.com/w/cpp/language/dynamic_cast – Asesh

+0

你鏈接那個頁面很有趣。我試圖這樣做,我得到了同樣的最終結果。也許你可以告訴我如何以這種方式考慮這些代碼? – polymorphism

+3

與問題無關 - 但爲什麼不使用某種多態的'GetShippingCost()'函數?這將是更可讀的方式,並會阻止你有這醜陋的條件/'dynamic_cast'混合。 – pSoLT

回答

5

的問題是你是如何定義的其他類:。

class Package 
{ 
    class Video_Games {}; //defined the classes 
    class Genius_Phone {}; 
    class Sausage {}; 
    class Albums {}; 

這些嵌套類型,裏面Package您已經定義了一個類Package::Video_Games其中沒有基類和沒有成員dynamic_cast表達式使用這些種類型,這是不能從Package衍生自:

 if (dynamic_cast<Video_Games*>(this)) 
      return 4.99; 

Package::shipping_cost()函數內名稱查找找到Package::Video_Games類型,這是一個沒有基類或成員的空類。對象是從未該類型,因爲Package::Video_Games不從Package衍生所以this指針Package*類型的不能指向Package::Video_Games類型的對象。

後來你定義一個新的類型名稱相同,即Package得出:

class Video_Games :public Package 
{ 

但是,這是一個新類型。 Video_GamesPackage::Video_Games不同。定義派生包對象時,它將使用此類型,而不是演員使用的類型。所以你的演員永遠不會成功。

您需要刪除嵌套類型的定義裏面Package和移動身體的Package::shipping_cost()功能的類外,其他所有類型被定義之後(所以編譯器已見過那些其他類型的定義和認識它們來源於Package)。

class Package 
{ 
    virtual double shipping_cost(); 
    // ... 
}; 

class Video_Games : public Package 
{ 
    // ... 
}; 

// Now you can define the virtual function: 
double Package::shipping_cost() 
{ 
    if (dynamic_cast<Video_Games*>(this)) 
    // ... 
} 

(但正如評論說,這是一個非常糟糕的方式來定義一個多態接口 - 它違背了虛函數的整個目的)

+0

謝謝喬納森,我正在慢慢消化你的工作,而且開始有意義。但是,我發現'shipcost'變量有問題,因爲它說現在它是未定義的。不知道爲什麼。我在Video_Games派生類中投入了'shipcost = shipping_cost()',但它仍然咬着我的腳,告訴我它沒有被定義,即使我在類Package中聲明瞭它。 – polymorphism

+0

好吧,我現在正在用這個程序玩鼴鼠,我把它編譯得很好,它顯示了我想要的運輸成本_exactly_。 但是,現在派生的Video_Games類給了我**運輸費用50.00計算總額,但它顯示4.99作爲船費,但是(這是正確的,它應該是什麼,而不是它是50.00在計算中隱含地使用)......所以現在我的問題已經被翻轉了。在我得到50.00作爲運輸成本和4.99計算出來之前。我的頭正在旋轉。 – polymorphism

+0

事實上,根據您的方法,所有計算均以50.00運輸成本估值完成,而所有顯示的成本均爲每個派生類準確。 – polymorphism

2

諷刺的是,鑑於您的用戶名,你錯過了使用多態性來提供正確的實現。相反,對類型切換,你可以調用虛方法,這將拿起正確的版本:

class Package { 
public: 
    virtual ~Package(); 
    virtual double shipping_cost() const; 
}; 
class Video_Games : public Package { 
public: 
    double shipping_cost() const override; 
}; 
class Genius_Phone : public Package { 
public: 
    double shipping_cost() const override; 
}; 
class Sausage : public Package { 
public: 
    double shipping_cost() const override; 
}; 

Package::~Package() {} 

double Package::shipping_cost() const 
{ 
    // Default implementation - may be overridden in subclasses 
    return 50.00; 
} 

double Video_Games::shipping_cost() const 
{ 
    return 4.99; 
} 

double Genius_Phone::shipping_cost() const 
{ 
    return 25.99; 
} 

double Sausage::shipping_cost() const 
{ 
    return 9.00; 
} 

我也警告說,使用貨幣值的浮點類型會導致你陷入困境 - 搜索堆棧溢出應找到解釋原因的答案。

+2

你錯過了遺傳。 – Jarod42

+0

非常正確 - 感謝@Lightness代我解決問題。 –

+1

好消息是,我確實聲明瞭「覆蓋」方法,以便編譯器會告訴你有關錯誤。 :-) –