我有一个名为Package的类,它包含来自Address
类的对象作为私有数据成员。还有其他课程,但由于没有必要,我没有展示。 address.h 和cpp文件提供给我,我无法更改它们。
我的问题出在 Package.cpp文件(第5行),我需要初始化Address对象,因为我正在使用合成但我不知道该怎么做。我知道我需要显式调用地址类的重载构造函数,但我不确定如何。当我在包构造函数之后放置:Address
时,我立即收到错误,指出Address
不是非静态数据成员或类Package
的基类。
Address.h
#ifndef ADDRESS_H
#define ADDRESS_H
#include <string>
class Address
{
public:
// constructor initializes data members
Address( const std::string &, const std::string &, const std::string &, const std::string &, int );
virtual ~Address() { } // virtual destructor
void setName( const std::string & ); // set name
std::string getName() const; // return name
void setAddress( const std::string & ); // set street address
std::string getAddress() const; // return street address
void setCity( const std::string & ); // set city
std::string getCity() const; // return city
void setState( const std::string & ); // set state
std::string getState() const; // return state
void setZIP( int ); // set sZIP code
int getZIP() const; // return ZIP code
void print() const; //print the data members
private:
// data members to store sender and recipient's address information
std::string name;
std::string streetAddress;
std::string city;
std::string state;
int ZIP;
};
#endif
Asssign3.cpp
// Assign3.cpp
// Processing Packages polymorphically.
#include <iostream>
#include <iomanip>
#include <vector>
#include "Package.h" // Package class definition
#include "TwoDayPackage.h" // TwoDayPackage class definition
#include "OvernightPackage.h" // OvernightPackage class definition
#include "Address.h" // Address class definition
using namespace std;
int main()
{
// create vector packages
vector < Package * > packages( 3 );
//create to and from address for the three objects
Address add1To("Lou Brown", "1 Main St", "Boston", "MA", 11111);
Address add1From("Mary Smith", "7 Elm St", "New York", "NY", 22222);
Address add2To("Lisa Klein", "5 Broadway Rd", "Indianapolis", "IN", 33333);
Address add2From("Bob George", "21 Pine Rd", "Miami", "FL", 44444);
Address add3to("Ed Lewis", "2 Oak St", "Boston", "MA", 55555);
Address add3From("Don Kelly", "9 Main St", "Denver", "CO", 66666);
// initialize vector with Packages
packages[ 0 ] = new Package(add1To, add1From, 8.5, .5 );
packages[ 1 ] = new TwoDayPackage(add2To , add2From, 10.5, .65, 2.0 );
packages[ 2 ] = new OvernightPackage(add3to , add3From, 12.25, .7, .25 );
cout << fixed << setprecision( 2 );
// print each package's information and cost
for ( size_t i = 0; i < packages.size(); i++ )
{
cout << "PACKAGE " << i + 1 << "\n";
packages[i]->print();
double cost = packages[ i ]->calculateCost();
cout << "\nShipping Cost: $" << cost << "\n\n";
}
}
Package.cpp错误
#include "Package.h"
#include "Address.h"
Package::Package(Address gets, Address sends, double weight, double costPerOunce)
{ // error no default constructor for class "Address"
double weight = weight;
double costPerOunce = costPerOunce;
}
double Package::getWeight()const
{
return weight;
}
Package.h
#ifndef PACKAGE_H
#define PACKAGE_H
#include "Address.h"
class Package
{
public:
Package(Address recipient, Address sender, double weight, double costPerOunce);
virtual ~Package();
double getWeight()const;
virtual void print() const;
double calculateCost()const;
private:
Address recipient;
Address sender;
double weight;
double costPerOunce;
};
#endif
答案 0 :(得分:0)
您有两个Address
成员,并且错误表明该类没有默认构造函数。因此,您需要初始化2个Address
班级成员。
Package::Package(Address gets, Address sends, double weight, double costPerOunce) :
recipient(gets),
sender(sends),
weight(weight),
costPerOunce(costPerOunce)
{
// I'm guessing you want to set your class members instead of these local variables...
//double weight = weight;
//double costPerOunce = costPerOunce;
}
不相关,但您可能希望使用const Address&
作为构造参数的类型。
:
部分称为iniatializer list。