c++ - 对类里面的成员访问培训感到困惑?

标签 c++ class syntax member

我有以下类(class):

#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);
 };

编辑:LOCKER 节点类

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!!!!!!!!!!
        }

    }

我检查了参数fine,判断是不是vip。但是,我不确定如何检查列表中的当前元素是否相同。我在相关线路上的上述努力没有奏效。我对语法有点困惑。谁能帮帮我?

谢谢!

最佳答案

你的 locker Node 类在哪里?问题可能在那里......

确定尝试替换它:

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

与:

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

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

关于c++ - 对类里面的成员访问培训感到困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906173/

相关文章:

c++ - 简单加密算法库 (SEAL) 和 seal::Ciphertext 变量

c++ - 如何模拟不同面数的骰子卷?

c++ - 模板类成员使用类本身作为模板参数时出现未定义类错误

c++ - 我可以在 Windows 98 上运行在 Visual Studio 2005 中编译的应用程序吗?

java - CardView动画

java - 如果我在 package1 中有带有 protected 方法的基类

php - 将 DB 对象传递到多次扩展的类中的简单方法

python - Python 3.2 中的 **kwargs 和 dict 有什么区别?

c - 结构之前的预期表达式错误

python - 尝试设置小数位数时语法无效