list.cpp `((TruckLoad*)this)->TruckLoad::pCurrent->TruckLoad::Package::pBox' cannot be used as a function
这个错误在文件4“list.cpp”中,我用颜色标出来。这个地方我很糊涂,不知道为什么出错也不知道怎么该,希望大家帮帮我,谢谢!
程序代码是这样的:
文件1:
#ifndef BOX_H
#define BOX_H
class Box {
public:
Box(double aLength=1.0,double aWidth=1.0,
double aHeight=1.0); //constructor
double volume() const; //calculate box volume
double getLength() const;
double getWidth() const;
double getHeight() const;
int compareVolume(const Box& otherBox) const; //compare volumes of boxes
private:
double length;
double width;
double height;
};
#endif
文件2
//box.cpp
#include "box.h"
//constructor
Box::Box( double aLength,double aWidth,double aHeight ) {
length=aLength>0.0 ? aLength : 1.0;
width=aWidth>0.0 ? aWidth : 1.0;
height=aHeight>0.0 ? aHeight : 1.0;
}
//calculate box volume
double Box::volume() const { return length*width*height; }
//getXXX functions
double Box::getLength() const { return length; }
double Box::getWidth() const { return width; }
double Box::getHeight() const { return height; }
//function to compare two box objects
//if the current box is greater than the argument,1 is returned
//if they are equal,0 is returned
//if the current box is less than the argument,-1 is returned
int Box::compareVolume(const Box& otherBox) const {
double vol1=volume();
double vol2=otherBox.volume();
return vol1>vol2 ? 1 : (vol1<vol2 ? -1 : 0);
}
文件3:
//list.h
#ifndef LIST_H
#define LIST_H
#include "box.h"
//class defining a list element
class TruckLoad {
public:
TruckLoad(Box* pBox=0,int count=1);
TruckLoad(const TruckLoad& load); //copy constructor
Box* getFirstBox();
Box* getNextBox();
void addBox(Box* pBox);
private:
//class defining a list element
class Package {
public:
Box* pBox;
Package* pNext;
void setNext(Package* pPackage);
Package(Box* pNewBox);
};
Package* pHead;
Package* pTail;
Package* pCurrent;
};
文件4:
//list.cpp
#include "box.h"
#include "list.h"
//package class definitions
//package constructor
TruckLoad::Package::Package(Box* pNewBox):pBox(pNewBox),pNext(0){}
//add package to end of list
void TruckLoad::Package::setNext(Package* pPackage) { pNext=pPackage; }
//TruckLoad class member definitions
//TruckLoad constructor
TruckLoad::TruckLoad( Box* pBox,int count ) {
pHead=pTail=pCurrent=0;
if( (count>0) && (pBox!=0) )
for( int i=0; i<count; i++ )
addBox(pBox+i);
}
//TruckLoad copy construcor
TruckLoad::TruckLoad(const TruckLoad& load) {
pHead=pTail=pCurrent=0;
if(load.pHead==0)
return;
Package* pTemp=load.pHead;
do {
addBox(pTemp->pBox);
}while(pTemp=pTemp->pNext);
}
//retrieve the first box
Box* TruckLoad::getFirstBox() {
pCurrent=pHead;
return pCurrent->pBox(); //就是这里错了
}
//retrieve the next box
Box* TruckLoad::getNextBox() {
if(pCurrent)
pCurrent=pCurrent->pNext(); //pCurrent is not null so set to next
else //pCurrent is null
pCurrent=pHead; //so set to the first list element
return pCurrent ? pCurrent->pBox() : 0;
}
//add a new Box to the list
void TruckLoad::addBox(Box* pBox) {
Package* pPackage=new Package(pBox); //create a package
if(pHead) //check list is not empty
pTail->pNext(pPackage); //add the new object to the tail
else //list is empty
pHead=pPackage; //so new object is the head
pTail=pPackage; //store its address as tail
}
文件5:
//main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
#include "box.h"
#include "list.h"
//function to generate a random integer 1 to count
inline int random(int count) {
return 1+static_cast<int>
(count*static_cast<double>(std::rand())/(RAND_MAX+1.0));
}
//find the box in the list with the largest volume
Box* maxBox(TruckLoad& load) {
Box* pBox=load.getFirstBox();
Box* pNextBox;
while(pNextBox=load.getNextBox())
if(pBox->compareVolume(*pNextBox)<0)
pBox=pNextBox;
return pBox;
}
int main() {
const int dimLimit=100;
std::srand((unsigned)std::time(0));
//create a list
TruckLoad load1;
//add 3 boxes to the list
for(int i=0; i<3; i++)
load1.addBox(new Box(random(dimLimit),random(dimLimit),random(dimLimit)));
Box* pBox=maxBox(load1);
cout<<endl
<<"The largest box in the first list is "
<<pBox->getLength() <<" by "
<<pBox->getWidth() <<" by "
<<pBox->getHeight() <<endl;
TruckLoad load2(load2);
cout<<endl
<<"The largest box in the second list is "
<<pBox->getLength() <<" by "
<<pBox->getWidth() <<" by "
<<pBox->getHeight() <<endl;
//add 5 more boxes to the second list
for(int i=0; i<5; i++)
load2.addBox(new Box(random(dimLimit),random(dimLimit),random(dimLimit)));
pBox=maxBox(load2);
cout<<endl
<<"The largest box in the extended second list is "
<<pBox->getLength() <<" by "
<<pBox->getWidth() <<" by "
<<pBox->getHeight() <<endl;
//count the number of boxes in the first list and display the count
Box* pNextBox=load1.getFirstBox();
int count=0;
while( pNextBox ) {
count++;
pNextBox=load1.getNextBox();
}
cout<<endl
<<"First list still contains "<<count<<" box objects."<<endl;
//delete the box objects in the free store
pNextBox=load2.getFirstBox();
while( pNextBox ) {
delete pNextBox;
pNextBox=load2.getNextBox();
}
system("pause");
return 0;
}