2013-06-03 50 views
1

我有以下类:有关会员在课堂上访问列车的疑惑?

#include <string> 
#include <cstdlib> 

using namespace std; 

class Locker{ 


public: 

int lockerId; 

string renterName; 

double monthlyRent; 

// The variable is true when the locker is a vip locker 
//if the locker is a regular locker then this variable is set to false 

bool isVip; 

bool isRentOverdue; 

Locker(){}; 

Locker(int id, string name, double rent, bool vip=0, bool overdue=0); 

bool operator==(Locker const &other); 
}; 

编辑:储物柜节点类

class LockerNode{ 
public: 

Locker objLocker; 
LockerNode *next; 


LockerNode(){ 
    next=0; 
}; 

LockerNode(Locker e, LockerNode *ptr=0){ 
    objLocker=e; 
    next=ptr; 
} 
    }; 

和实施:

#include <sstream> 
#include "Locker.h" 

#include <iostream> 

using namespace std; 

Locker::Locker(int id, string name, double rent, bool vip, bool overdue){ 
lockerId=id; renterName=name; monthlyRent=rent; isVip=vip; isRentOverdue=overdue; 
} 

bool Locker::operator==(Locker const &other){ 
if(lockerId==other.lockerId && renterName==other.renterName && monthlyRent==other.monthlyRent && isVip==other.isVip && isRentOverdue==other.isRentOverdue) 
    return true; 
else return false; 
} 

我在一个函数下面的代码,试图跟踪链表中对象的数量,并根据它们的数量和数量对它们执行一些操作他们的属性。我通过e这是一个新创建的对象。如果一个对象具有属性vip = true,那么我需要将它放在其他非vip对象之前,除非已经有一个vip对象,在这种情况下,它就在它之后。因此,下面的代码:

int count = 0; 
LockerNode *p = head; 

for(;p!=0;count++, p=p->next) { 

    if(count == 1) { 
     if (e.isVip) { 
      if(p->isVip) // !!!!!!!!!Issue here!!!!!!!!!! 
     } 

    } 

我检查了参数罚款,以确定它是否是VIP。但是,我不确定如何检查我在列表中的相同元素。我在上述问题上的努力没有奏效。对于语法我有点困惑。谁能帮我吗?

谢谢!

回答

1

你的储物柜节点类在哪里?问题可能是有...

OK尝试更换此:

if(p->isVip) // !!!!!!!!!Issue here!!!!!!!!!! 

有:

if (p->objLocker.isVip) {//true/false for this node 

p是一个指针accesing他的成员是与 - > 但objlocker不是指针访问他的成员是与“。”

+0

道歉,忽略包括它。一秒钟见原文。 –

+0

啊,谢谢澄清句。不过,我想我仍然需要e.isVip。基本上,e.isVip会检查CURRENT锁柜对象是否为VIP(即我正在寻找的地方)。 p是一个指针,用于在列表中搜索已存在的储物柜。 –

+1

是的,我现在看到我删除的东西我肩膀重要的事情,因为你有这个想法,你现在可以自己解决问题 – petric