我的程序应该打印来自 EndPoint toString 方法的“to”和“from”地址,但我不太清楚如何实现它。这是我的代码。如何在 Package::Package 构造函数中获取 toString 方法来打印 EndPoint 的 toString 方法的内容?
// 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”类。
我的错误在于我如何让我的 Package 构造函数能够包含来自我的 EndPoint 类的信息。由于 main 方法在 Package 类必须位于的位置(EndPoint、EndPoint、Weight、Cost)被格式化,但它不会像那样编译。我想我只是不明白如何将 EndPoint 信息发送到 Package 对象。
这是我的错误:
- 没有构造函数实例
"Package::Package" matches the argument list argument types are: (EndPoint, EndPoint, double, double)
Error C2440 'initializing': cannot convert from 'initializer list' to 'Package'
Error C3861 'setprecision': identifier not found
包.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
包.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();
}
主文件
#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 学习了......),并且我已经让程序按预期编译和工作,除了一个小但重要的问题。