2013-06-03 94 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