如何在c ++中将null返回给用户定义的类

时间:2014-12-05 19:34:52

标签: c++ null

我已经定义了一个与我也创建的Card类一起使用的Queue类,函数方法返回应该在调用时返回Card,但是如果没有卡则应该返回null。这样做的问题是,当我尝试nullNULL'\0'时,我一直收到错误。

对于'\0'我得到一个char to card错误,对于NULL我得到一个很长的卡错误,奇怪的部分是null不被识别为关键字而被忽略。

我现在想知道是否必须以某种方式在Cards类中定义null,或者它是否完全不同。以下是我为Queue类使用的代码,以备您需要:

class Queue{
    Card cards[52];
    int first;
    int last;
    int count;

public:
    Queue();

    void insert(Card insert);
    Card remove();
    int getSize();
};

Queue::Queue(){
    first = last = count = 0;
}

Card Queue::remove(){
    if(count == 0)
        return null;
    Card temp = cards[last];
    last++;
    count--;
    if (last > 0)
        last = 51;
    return temp;
}

编辑:

class Queue{
    Card *cards;
}
Queue::Queue(){
    cards = new Card[52];
}
Card Queue::remove(){
    if(count == 0)
        return nullptr;
}

0 个答案:

没有答案