出于某种原因,我遇到了一些错误,但我认为这与我评论过的代码有关。有人可以帮助我取悦我,因为我一遍又一遍地得到同样的错误。我的代码应该回答以下问题://创建一个类调用的包,它应该有包的权重和包跟踪号的成员变量。添加函数以访问这些变量并在构造函数中设置它们。添加一个函数来计算运费,使用公式cost = $ 3 per pound
//为盒形包创建一个名为Box的派生类。它应该以英寸为单位存储高度,宽度和深度的尺寸。 Redfine计算运输的功能,如果最长+周长大于108英寸,则将30添加到正常成本
//编写一个创建方框的主函数,指定重量高度宽度和深度并打印运费
#include <iostream>
#include <string>
using namespace std;
namespace shipping
{
const double COST_PER_POUND=3.0;
class Package
{
public:
Package();//default constructor
Package(double new_weight, string new_tracking_num);
double get_weight() const;//accessors
string get_tracking_num() const;//accessors
void set_weight(double new_weight);
void set_tracking_num(string new_tracking_num);
double cal_shipping() const;
private:
double weight;
string tracking_num;
};
}
#include <string>
#include "Package.h"
using namespace std;
namespace shipping
{
Package::Package(): tracking_num(""), weight(0)
{
}
Package::Package(double new_weight, string new_tracking_num):
tracking_num(new_tracking_num), weight(new_weight)
{
}
double Package::get_weight() const
{
return weight;
}
string Package::get_tracking_num() const
{
return tracking_num;
}
void Package::set_weight(double new_weight)
{
weight=new_weight;
}
void Package::set_tracking_num(string new_tracking_num)
{
tracking_num= new_tracking_num;
}
double Package::cal_shipping() const
{
return weight*COST_PER_POUND;
}
}
#include <string>
#include "Package.h"
using namespace std;
namespace shipping
{
class Box : public Package{
public:
Box();
Box(double new_weight, string new_tracking_num,
double new_width, double new_height,double new_depth);
void set_dimension(double new_width, double new_height, double new_depth);
void get_dimensions(double &cur_width, double &cur_height, double &cur_depth);
double calc_shipping() const;
private:
double width, height, depth;
};
}
#include "Box.h"
#include <string>
using namespace std;
namespace shipping
{
Box::Box(): Package(),height(0),depth(0),width(0)
{
}
//my error comes here
Box(double new_weight, string new_tracking_num,
double new_width,double new_height,double new_depth):
Package(new_weight, new_tracking_num), height(new_height), depth(new_depth), width(new_width)
{
}
void Box::set_dimension(double new_width, double new_height, double new_depth)
{
width=new_width;
height=new_height;
depth=new_depth;
}
void Box::get_dimensions(double &cur_width, double &cur_height, double &cur_depth)
{
cur_width=width;
cur_height=height;
cur_depth=depth;
}
double calc_shipping() const{
double longest=width;
double girth=(height*2)+(2*depth);
if(height>longest)
{
longest=height;
girth=(width*2)+(2*depth);
}
if(depth>longest)
{
longest=depth;
girth=(height*2)+(2*width);
}
if(longest+girth>108)
{
return Package::cal_shipping()+30;
}
else
{
return Package::calc_shipping();
}
}
}
答案 0 :(得分:1)
我在您的代码中看到了以下问题:
在构造函数中,您以与它们在类中出现的顺序不同的顺序初始化成员变量。
示例:
在班级Package
中,您有变量:
double weight;
string tracking_num;
在构造函数中,首先初始化tracking_num
然后weight
。例如:
Package::Package(): tracking_num(""), weight(0)
{
}
它会切换初始化顺序,因为这是一项要求。由于编译器正在改变代码的执行顺序,因此它会向您发出警告。
您在Package
以及Box
中有更多这些示例。
您忘了在几个函数中添加类范围Box::
。
Box(double new_weight, string new_tracking_num,
double new_width, double new_height,double new_depth):
需要
Box::Box(double new_weight, string new_tracking_num,
double new_width, double new_height,double new_depth):
和
double calc_shipping() const{
需要
double Box::calc_shipping() const{
您在三个地方拼写错误calc_shipping()
并使用cal_shipping
(缺少&#39; c&#39;)。