c++:循环时出现TypeError,但在循环工作之前-(已修改)LinkedListClass迭代

c++: TypeError at loop - but before the loop work it - (modifided)LinkedListClass iterate

本文关键字:循环 迭代 LinkedListClass 修改 TypeError c++ 工作      更新时间:2023-10-16

我有一个arduino的c++项目,我不会使用std::包,这也是有原因的。因此,我四处寻找Arduino的LinkedList类,并在github上创建了一个类:https://github.com/ivanseidel/LinkedList.

我想迭代列表中的所有。为此,我有这样的代码:

uint8_t memory::write_data(uint8_t id) {
    // here is all ok -> working, but not in the loop that follows.
    uint16_t temp_size = (uint16_t) blocks->get(id);
    uint8_t blocks = (uint8_t) (temp_size / (uint8_t)255); // => 256 = 1byte - 1 => (0-255) => range (1-256);
    if (temp_size < 255) blocks++;
    uint16_t pointer = 0; // With pointer means the pointer (in byte) for the "memory(eeprom)" location 
    for (uint8_t i=0; i<id; i++) {
        Serial.print("Index:");
        Serial.println(blocks.get(i)); // -> instand of int is going to go error
        // !! ERROR: request for member 'get' in 'blocks', which is of non-class type 'uint8_t {aka unsigned char}'
        //pointer += i;   
    }
    Serial.print("ALL:");
    Serial.println(pointer);
}

错误:请求"blocks"中的成员"get",该成员属于非类类型"uint8_t{aka unsigned char}"

此时来自库的代码是:

template<typename E, typename T>
T LinkedList<E, T>::get(E index){
    ListNode<T> *tmp = getNode(index);
    return (tmp ? tmp->data : T());
}

而且。。。

template<typename E, typename T>
ListNode<T>* LinkedList<E, T>::getNode(E index){
    int _pos = 0;
    ListNode<T>* current = root;
    // Check if the node trying to get is
    // immediatly AFTER the previous got one
    if(isCached && lastIndexGot <= index){
        _pos = lastIndexGot;
        current = lastNodeGot;
    }
    while(_pos < index && current){
        current = current->next;
        _pos++;
    }
    // Check if the object index got is the same as the required
    if(_pos == index){
        isCached = true;
        lastIndexGot = index;
        lastNodeGot = current;
        return current;
    }
    return false;
}

LinkedList定义为:

LinkedList <uint8_t, uint16_t> *blocks = new LinkedList<uint8_t, uint16_t>();

我试着想办法解决这个问题,但我做不到。有人能告诉我这个问题吗?谢谢

uint8_t是简单值,而不是类。它没有.get()方法。

你可以简单地写Serial.println(blocks[i]);如果您重写行

uint8_t blocks = (uint8_t) (temp_size / (uint8_t)255);

带有

uint8_t* blocks = (uint8_t*) (temp_size / (uint8_t)255);