【问题标题】:Control may reach end of non-void function Xcode c++控制可能到达非空函数 Xcode c++ 的末尾
【发布时间】:2014-10-09 20:22:25
【问题描述】:

为什么会出现这个错误?? 在我将此文件添加到 Xcode 之前,这不是错误,不知道出了什么问题。 在我的队列类中删除函数:

class queue {

private:
    point* Q[MSIZE];

    int front, rear, size;

public:
    queue() {
        // initialize an empty queue
        front = 0; rear = 0; size = 0;
        for (int j=0; j < MSIZE; ++j)
           Q[j] = 0;
    }

    void insert(point* x) {
        if (size != MSIZE) {
            front++; size++;
            if (front == MSIZE) front = 0;
            Q[front] = x;
        }
    }

    point del() {
        if (size != 0) {
            rear++; if (rear == MSIZE) rear = 0;
            point temp(Q[rear]->getx(), Q[rear]->gety());
            size--;
            return temp;
        };
    } // error "Control may reach end of non-void function" on this line
}

【问题讨论】:

    标签: xcode queue


    【解决方案1】:

    函数del 返回一个point。如果 size != 0,则没有 return 语句。在其他情况下您需要返回一些东西,否则函数可以结束而不返回 point,这会产生错误。

    解决此问题的一种方法是:return null

    bool del(point& pointRef)
    {
        if (size != 0)
        {
            rear++;
            if (rear == MSIZE)
            {
                rear = 0;
            }
            pointRef = Q[rear];
            size--;
            return true;
        }
        return false;
    }
    

    然后在这个函数之外,如果你得到了错误,你就知道什么都没有发生。如果你是真的,那么你知道你有一个指向已删除point 的指针。

    调用示例:

    point aPoint;
    bool result;
    
    result = del(aPoint);
    if(result)
    {
        // do stuff with aPoint
    }
    else
    {
        // the queue was empty
    }
    

    【讨论】:

    • 我试过这个,它给了我错误:“没有可行的从'long'到'point'的转换”返回NULL
    • return nil; 有效吗?我现在不在 Xcode,所以我不能检查这个。你确定你在 C++ 中?代码将位于 .mm 文件中。
    • 哦,在 C++ 中不能返回 null。将修复我的答案。等一下。
    • 当我调用这个函数时,我想访问这个点并查看它的邻居。布尔值不允许我这样做
    • 查看传递引用的作用。你给这个函数一个point的引用,然后bool告诉你它是否有效。我们通过函数的参数返回point
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    相关资源
    最近更新 更多