| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 626 人关注过本帖
标题:[求助]请教关于类成员访问的问题
只看楼主 加入收藏
叮叮当
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-1-2
收藏
 问题点数:0 回复次数:2 
[求助]请教关于类成员访问的问题
大家好,请教一下,我在做一道题的时候,提示错误:
list.cpp `((TruckLoad*)this)->TruckLoad::pCurrent->TruckLoad::Package::pBox' cannot be used as a function
这个错误在文件4“list.cpp”中,我用颜色标出来。这个地方我很糊涂,不知道为什么出错也不知道怎么该,希望大家帮帮我,谢谢!
程序代码是这样的:
文件1:
//box.h
#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;
}

搜索更多相关主题的帖子: 成员 访问 
2007-04-13 21:17
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
    return pCurrent-&gt;pBox(); //pBox是个数据成员,对它访问不加扩号

Fight  to win  or  die...
2007-04-13 21:36
叮叮当
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-1-2
收藏
得分:0 
原来如此,非常感谢!现在可以通过编译了

还有最后一个地方
pTail->pNext(pPackage);
改成
pTail->setNext(pPackage);
就好了,多谢!
2007-04-13 21:46
快速回复:[求助]请教关于类成员访问的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042206 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved