简体   繁体   English

“不应忽略的无效值”尝试对数组进行排序时

[英]“void value not ignored as it ought to be” When trying to sort an array

I'm new to programming and trying to program a sensor.我是编程新手并尝试对传感器进行编程。 I'm sorting the array that I plot the input from the sensor into.我正在对来自传感器的输入 plot 的数组进行排序。 But I'm running into a problem when sorting it, it gives me this error:但是我在排序时遇到了问题,它给了我这个错误:

exit status 1退出状态 1
void value not ignored as it ought to be"无效值没有被忽略,因为它应该是”

Here's my code:这是我的代码:

#include <ArduinoSort.h>

int a[10];
int b[10];
int sensor1 = A0;
int sensor2 = A1;
int display1;
int display2;
int sort[10];

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
}

void loop() {
    // put your main code here, to run repeatedly:
    delay(100);
    Serial.print("Sensor 1: [");

    for (int i = 0; i < 11; i++) {
        display1 = analogRead(sensor1);
        a[i] = display1;
        sort[i] = sortArray(a, 10);
        if (i < 10) {
            Serial.print(String(sort[i]) + ",");
        } else {
            Serial.print(String(sort[i]));
        }
    }

    Serial.print("]");
    Serial.println();

    Serial.print("Sensor 2: [");
    for (int i = 0; i < 11; i++) {
        display2 = analogRead(sensor2);
        b[i] = display2;
        if (i < 10) {
            Serial.print(String(b[i]) + ",");
        } else {
            Serial.print(String(b[i]));
        }

    }
    Serial.print("]");
    Serial.println();
    Serial.println();

    //String hej = "Sensor 1"+ String(display1);
    //Serial.println(hej);
}

and, on this line I get the error:并且,在这一行我得到了错误:

sort[i] = sortArray(a,10);

The sortArray() function returns nothing ie void . sortArray() function 什么也不返回,即void

It sorts the passed array in-place so you need to call it like this:它对传递的数组进行就地排序,因此您需要像这样调用它:

sortArray(a, 10); // no return value assignment

As pointed out by Ben Voigt , you have not initialized a properly so you need to first collect all the values and then sort it after the loop.正如Ben Voigt指出的,您尚未正确初始化a ,因此您需要首先收集所有值,然后在循环后对其进行排序。

Here's your loop (after correction of sortArray after loop):这是你的循环(在循环后更正sortArray之后):

for (int i = 0; i < 11; i++) {  // 0-10 i.e. 11 iterations
    a[i] = analogRead(sensor1); // read values in the array
    // ...
}

sortArray(a, 10); // sort after the loop

Another problem is that your loop's condition is i < 11 which means the iterations from 0 to 10 ie 11 iterations.另一个问题是您的循环条件是i < 11 ,这意味着从 0 到 10 的迭代,即 11 次迭代。 But, the arrays you're using are of size 10 ie 0 to 9 locations as C++ arrays are ZERO-based.但是,您使用的 arrays 的大小为 10,即 0 到 9 个位置,因为 C++ arrays 是基于零的。 So, this is causing out-of-bounds access resulting in Undefined Behavior .因此,这会导致越界访问导致Undefined Behavior

So, your loop iterations and the array sizes should match ie 10 iterations and 10 memory locations to write to.因此,您的循环迭代和数组大小应该匹配,即 10 次迭代和 10 个 memory 位置要写入。

It's better to use a constant and use that at all places like this:最好使用一个常量并在所有地方都使用它:

const int SIZE = 10;

int a[SIZE] = {0}; // initialize if it's not an overhead for arduino

for (int i = 0; i < SIZE; i++) {
    a[i] = analogRead(sensor1);
    // ...
}

sortArray(a, SIZE);

You have a somewhat bigger problem with this code.这段代码有一个更大的问题。

You simply cannot "print outputs in sorted order" while you are still collecting them.当您仍在收集它们时,您根本无法“按排序顺序打印输出”。 You don't know whether the "smallest" right now should be printed, or wait because an even smaller one is forthcoming.您不知道现在是否应该打印“最小的”,或者等待,因为即将出现更小的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM