2016-11-04 53 views
-3

我的程序應該打印源自EndPoint toString方法的「to」和「from」地址,但我無法弄清楚如何實現它。這是我的代碼。如何獲取Package :: Package構造函數中的toString方法以打印EndPoint的toString方法的內容?關於繼承,隱私和具有多個類和標題的裝運程序中的對象的C++問題

// ShippingProgram.cpp : 
// 

#include "stdafx.h" 
#include <iomanip> 
#include <stdexcept> 
#include <sstream> 
#include <iostream> 
#include "Package.h" // Package class definition 
using namespace std; 

// constructor 

EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) { 
    name = nameInfo; 
    address = addressInfo; 
    city = cityInfo; 
    state = stateInfo; 
    zip = zipInfo; 
} 


string EndPoint::toString() const { 
    ostringstream output; 
    output << fixed << setprecision(2); // two digits of precision 
    output << name << "\n" << address << "\n" << 
     city << ", " << state << " " << zip; 
    return output.str(); 
} 


Package::Package(EndPoint senderInfo, EndPoint receiverInfo, double weightInfo, double costPerOzInfo) { 

    weight = weightInfo; // should validate      
    costPerOz = costPerOzInfo; 
} 


void Package::calculateCost(double) 
{ 
} 

double Package::calculateCost() const { 
    return weight * costPerOz; 
} 


string Package::toString() const { 
    ostringstream output; 
    output << fixed << setprecision(2); // two digits of precision 
    output << "\nFrom:\n" << senderInfo.toString() << "\n\nTo:\n" << receiver << 
     "\n\nWeight: " << weight << endl << 
     "\nShipping cost:\n" << calculateCost(); 
    return output.str(); 
} 

主要方法:

#include <iostream> 
#include <iomanip> 
#include "stdafx.h" 
//#include "TwoDayPackage.h" 
//#include "OvernightPackage.h" 
#include "Package.h" 
using namespace std; 

int main() 
{ 
    // Three address records. 

    EndPoint homer{ "Homer Simpson", "742 Evergreen Terrace", "Springfield", 
     "FL", 32401 }; 

    EndPoint donald{ "Donald Duck", "1313 Webfoot Walk", "Duckburg", 
     "CA", 95501}; 

    EndPoint kermit{ "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 }; 

    // This calls the base class constructor (regular fee). 

    Package regular{ homer, donald, 25.0, 0.20 }; 

    // Defines output precision for floating point numbers (iomanip). 

// cout << fixed << setprecision(2); 

    // Prints package parameters. 

    cout << "Regular package processed." << endl; 
    cout << regular.toString(); 
    cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl; 
    cout << homer.toString(); 
    // First derived class (two-day fee added). 

    /* TwoDayPackage twoday{ donald, kermit, 17.5, 0.20, 2.0 }; 

    cout << "Two-day package processed." << endl; 
    cout << twoday.toString(); 
    cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl; 

    // Second derived class (overnight fee added). 

    OvernightPackage overnight{ kermit, homer, 14.2, 0.20, 0.50 }; 

    cout << "Overnight package processed." << endl; 
    cout << overnight.toString(); 
    cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl; 
    */ 
    } 

該項目需要,我創建繼承裝運計劃。它必須包含一個私有「EndPoint」類,它包含發送者和接收者信息以及一個編譯所有內容並將其放入字符串的「Package」類。

我的錯誤是如何在世界上我得到我的包構造函數能夠包含我的EndPoint類的信息。由於主要方法的格式必須是Package類(EndPoint,EndPoint,Weight,Cost),但它不能像那樣編譯。我想我只是不明白如何將EndPoint信息發送到Package對象。

這裏是我的錯誤:

  1. 構造的無實例"Package::Package" matches the argument list argument types are: (EndPoint, EndPoint, double, double)
  2. Error C2440 'initializing': cannot convert from 'initializer list' to 'Package'
  3. Error C3861 'setprecision': identifier not found

Package.h

#pragma once 
#ifndef PACKAGE_H 
#define PACKAGE_H  
#include <string> 
#include <iostream> 
using namespace std; 

class EndPoint { 
public: 
    EndPoint(const std::string&, const std::string&, const std::string&, const std::string&, int = 0.0); 

    void setName(const std::string&); 
    std::string getName() const; 

    void setAddress(const std::string&); 
    std::string getAddresss() const; 

    void setCity(const std::string&); 
    std::string getCity() const; 

    void setState(const std::string&); 
    std::string getState() const; 

    void setZip(int); 
    int getZip() const; 

    string toString() const; 
protected: 
    std::string name; 
    std::string address; 
    std::string city; 
    std::string state; 

    int zip; 
}; 
class Package { 
public: 
    string toString() const; 
    Package(const std::string&, const std::string&, double = 0.0, double = 0.0); 

    void setSender(const std::string&); 
    std::string getSender() const; 

    void setReceiver(const std::string&); 
    std::string getReceiver() const; 

    void setWeight(double); 
    double getWeight() const; 

    void setCostPerOz(double); 
    double getCostPerOz() const; 

    void calculateCost(double); 
    double calculateCost() const; 

    double calculateCost(double weight, double costPerOz) 
    { 
     double shipping; 
     shipping = weight * costPerOz; 

     cout << "The Base Cost = " << shipping << endl << endl; 
     return shipping; 
    } 



protected: 
    std::string sender; 
    std::string receiver; 
    double weight; // gross weekly sales 
    double costPerOz; // commission percentage 
}; 

#endif 

Package.cpp

#include "stdafx.h" 
#include <iomanip> 
#include <stdexcept> 
#include <sstream> 
#include "Package.h" // Package class definition 
using namespace std; 

// constructor 

EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) { 
    name = nameInfo; 
    address = addressInfo; 
    city = cityInfo; 
    state = stateInfo; 
    zip = zipInfo; 
} 

void EndPoint::setName(const string& nameInfo) { 
    name = nameInfo; 
} 

string EndPoint::getName() const { return name; } 

void EndPoint::setAddress(const string& addressInfo) { 
    address = addressInfo; 
} 

string EndPoint::getAddresss() const { return address; } 

void EndPoint::setCity(const string& cityInfo) { 
    city = cityInfo; 
} 

string EndPoint::getCity() const { return city; } 

void EndPoint::setState(const string& stateInfo) { 
    state = stateInfo; 
} 

string EndPoint::getState() const { return state; } 

void EndPoint::setZip(int zipInfo) { 
    zip = zipInfo; 
} 

int EndPoint::getZip() const { 
    return zip; 
} 

string EndPoint::toString() const { 
    ostringstream output; 
    output << fixed << setprecision(2); // two digits of precision 
    output << name << "\n" << address << "\n" << 
     city << ", " << state << " " << zip; 
    return output.str(); 
} 
string EndPoint::getState() const { return state; } 
Package::Package(const string& senderInfo, const string& receiverInfo, double weightInfo, double costPerOzInfo) { 
    sender = senderInfo; // should validate        
    receiver = receiverInfo; // should validate         
    weight = weightInfo; // should validate      
    costPerOz = costPerOzInfo; 
} 


void Package::setSender(const string& senderInfo) { 
    sender = senderInfo; // should validate 
} 


string Package::getSender() const { return sender; } 


void Package::setReceiver(const string& receiverInfo) { 
    receiver = receiverInfo; // should validate 
} 


string Package::getReceiver() const { return receiver; } 



void Package::setWeight(double weightInfo) { 
    if (weightInfo < 0.0) { 
     throw invalid_argument("The package weight must be >= 0.0"); 
    } 

    weight = weightInfo; 
} 


double Package::getWeight() const { return weight; } 


void Package::setCostPerOz(double costPerOzInfo) { 

    costPerOz = costPerOzInfo; 
} 


double Package::getCostPerOz() const { 
    return costPerOz; 
} 


double Package::calculateCost() const { 
    return weight * costPerOz; 
} 


string Package::toString() const { 
    ostringstream output; 
    output << fixed << setprecision(2); // two digits of precision 
    output << "From:\n" << sender << "\n\nTo:\n" << receiver << 
     "\n\nWeight: " << weight << endl << 
     "\nShipping cost: " << calculateCost(); 
    return output.str(); 
} 

Main.cpp的

#include <iostream> 
#include <iomanip> 
#include "stdafx.h" 
//#include "TwoDayPackage.h" 
//#include "OvernightPackage.h" 
#include "Package.h" 
using namespace std; 

int main() 
{ 
    // Three address records. 

    EndPoint homer{ "Homer Simpson", "742 Evergreen Terrace", "Springfield", 
     "FL", 32401 }; 

    EndPoint donald{ "Donald Duck", "1313 Webfoot Walk", "Duckburg", 
     "CA", 95501}; 

    EndPoint kermit{ "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 }; 

    // This calls the base class constructor (regular fee). 

    Package regular{ homer, donald, 25.0, 0.20 }; 

    // Defines output precision for floating point numbers (iomanip). 

    cout << fixed << setprecision(2); 

    // Prints package parameters. 

    cout << "Regular package processed." << endl; 
    cout << regular.toString(); 
    cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl; 

    // First derived class (two-day fee added). 

    /* TwoDayPackage twoday{ donald, kermit, 17.5, 0.20, 2.0 }; 

    cout << "Two-day package processed." << endl; 
    cout << twoday.toString(); 
    cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl; 

    // Second derived class (overnight fee added). 

    OvernightPackage overnight{ kermit, homer, 14.2, 0.20, 0.50 }; 

    cout << "Overnight package processed." << endl; 
    cout << overnight.toString(); 
    cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl; 
    */ 
    } 

我已經爲我想剛剛拿到第一部分潛水進入休息之前,在這裏工作註釋掉的代碼塊。

編輯:

謝謝大家的建議!我做了一些編輯並取出了大量額外的代碼(getter和setter,我用java學到了...),並且我已經獲得了程序編譯和工作的意圖,除了一個小而重要的問題。

+1

擁有了iomanip請[編輯]您的問題以提供[mcve]。 –

+0

順便說一句,你不需要使用*預編譯頭文件*或'stdafx.h'。對於較小的項目,建設時間不會減少,特別是在開發時。 –

+0

我建議將'EndPoint'放入單獨的頭文件和源文件中。 –

回答

1

構造 「套票:套餐」 無實例相匹配的參數列表 參數類型有:(端點,即端點,雙,雙)

在你的代碼:
Package regular{ homer, donald, 25.0, 0.20 };
你將錯誤的變量傳遞給構造函數的參數,該參數是第一個和第二個參數的端點對象

您所擁有的是:

Package(const std::string&, const std::string&, double = 0.0, double = 0.0); 

它接受第一個和第二個參數的std :: string對象。

無法從「初始化列表」轉換爲「包

固定問題1將解決這個問題

我不知道爲什麼你得到第三一個,因爲你在你的主

+0

在這個項目的說明中,它表示將端點設置爲私有(我沒有這樣做,因爲它打破了一切)。我只是不明白。如果我改變了所有與端點一起工作的方法,我就會遇到更多的錯誤類型錯誤,並且它說沒有默認的端點構造函數,但是如果我創建另一個錯誤......我將我的頭髮拉出來。 如何將端點傳遞給包構造函數? –

+0

@BrenBailey對不起,但我昨天睡着了,要傳遞端點到構造函數,你必須讓它接受一個參數的端點,就像你在字符串中做的那樣,或者像這樣加載包(const EndPoint&ep,const EndPoint&,double,double ),你可以順便重載它。 –