不能返回指向结构体的指针

C++ : Cannot return a pointer to a struct

本文关键字:指针 结构体 返回 不能      更新时间:2023-10-16

我试图有一个函数返回一个指向链表中最小单元格的指针,其中单元格是一个结构体。这个函数给了我错误,说函数缺少类型说明符。如有任何帮助,不胜感激。

. h文件
private:
// TODO: Fill this in with the implementation of your doubly-linked list
// priority queue.  You can add any fields, types, or methods that you
// wish.
struct Cell {
    string value;
    Cell * next;
    Cell * prev;
};
int count;
Cell * root;
void clear();
Cell * getSmallestCell();

. cpp文件
Cell * DoublyLinkedListPriorityQueue::getSmallestCell() {
Cell * smallest = root;
for (Cell * i = root; i != NULL; i = i->next) {
    if (i->value < smallest->value) {
        smallest = i;
    }
}
return smallest;

}

代替

Cell* DoublyLinkedListPriorityQueue::getSmallestCell()

应该是

DoublyLinkedListPriorityQueue::Cell* DoublyLinkedListPriorityQueue::getSmallestCell()

或(c++ 11起)

auto DoublyLinkedListPriorityQueue::getSmallestCell() -> Cell *

作为CellDoublyLinkedListPriorityQueue的内类型