数组索引超出范围异常C#(Array Index out of range exception C#)
我试图在数组的平均值上找到数组中最接近的数字。 阵列将在主程序中确定。 这是当前的代码:
public static void Main() { double[] numbers = { 1, 2, 3, 2, 5 }; double m1 = Miidi(numbers); double m2 = Miidi(new double[] { 1 }); double m3 = Miidi(new double[] { 3, 3 }); double m4 = Miidi(new double[] { }); Console.WriteLine(m1); Console.WriteLine(m2); Console.WriteLine(m3); Console.WriteLine(m4); } public static double Miidi(double[] numbers) { double average = 0; average = numbers.Average(); double nearest = FindNearest(numbers, average); return nearest; } public static double FindNearest(double[] t, double avg) { double searchValue = avg; double currentNearest = t[0]; double currentDiff = currentNearest - searchValue; for (int i = 0; i < t.Length; i++) { double diff = t[i] - avg; if (diff < currentDiff) { currentDiff = diff; currentNearest = t[i]; } } return currentNearest; }
}
我得到一个超出范围异常的数组索引。 我尝试了你们提供的方法,并在循环中改变了<= to <,但我仍然得到异常。 我提供了主要程序以便澄清。
I'm trying to find the closest number in an array when compared to the array's average. The array will be determined in the main program. Here's the current code:
public static void Main() { double[] numbers = { 1, 2, 3, 2, 5 }; double m1 = Miidi(numbers); double m2 = Miidi(new double[] { 1 }); double m3 = Miidi(new double[] { 3, 3 }); double m4 = Miidi(new double[] { }); Console.WriteLine(m1); Console.WriteLine(m2); Console.WriteLine(m3); Console.WriteLine(m4); } public static double Miidi(double[] numbers) { double average = 0; average = numbers.Average(); double nearest = FindNearest(numbers, average); return nearest; } public static double FindNearest(double[] t, double avg) { double searchValue = avg; double currentNearest = t[0]; double currentDiff = currentNearest - searchValue; for (int i = 0; i < t.Length; i++) { double diff = t[i] - avg; if (diff < currentDiff) { currentDiff = diff; currentNearest = t[i]; } } return currentNearest; }
}
I'm getting an array index out of range exception. I tried the methods you guys provided and changed the <= to < in the loop, but I'm still getting the exception. I provided the main program for clarification.
原文:
最满意答案
您需要从
a.out
命令中删除-c
:a.out: test.o extra.o gcc $(T) test.o extra.o
或更好:
a.out: test.o extra.o gcc $(T) -o $@ test.o extra.o
或者,还是更好:
extra: test.o extra.o gcc $(T) -o $@ test.o extra.o
错误消息通常是因为您在命令行上使用
-c
指定了-lm
内容,但是您没有在此处执行此操作。 OTOH,你用-c
选项列出目标文件 - 它将生成警告:gcc -ansi -pedantic -Wall -Werror -c test.o extra.o ^ this one, here
那些
.o
文件是链接器输入,但您没有链接。 删除-c
,您将生成a.out
。 那应该没问题。我想这是我第一次看到用于构建
a.out
的makefile。 极端不寻常 - 并非完全错误,但绝对不是你通常使用的方式。 它具有内置规则,可以从单个源文件构建程序,例如example.c
example
。 通常,您根据其中一个源文件为程序提供有意义的名称。 请注意,创建程序test
通常是一个坏主意; 在典型的shell中有一个标准的test
命令,混乱很猖獗。You need to remove the
-c
from thea.out
command:a.out: test.o extra.o gcc $(T) test.o extra.o
or, better:
a.out: test.o extra.o gcc $(T) -o $@ test.o extra.o
or, still better:
extra: test.o extra.o gcc $(T) -o $@ test.o extra.o
The error message is normally because you specify something like
-lm
on a command line with-c
, but you're not doing that here. OTOH, you are listing object files with the-c
option — that'll generate the warning:gcc -ansi -pedantic -Wall -Werror -c test.o extra.o ^ this one, here
Those
.o
files are the linker inputs, but you're not linking. Drop the-c
and you will generatea.out
. That should proceed OK.I think this is the first time I've seen a makefile used to build
a.out
. It is unusual in the extreme — not precisely wrong, but definitely not the way you normally usemake
. It has built-in rules to build programs from single source files, such asexample
fromexample.c
. Normally, you give a program a meaningful name based on one of the source files. Note that creating a programtest
is usually a bad idea; there is a standardtest
command built into the typical shell and confusion is rampant.
相关问答
更多-
我认为你对编译器如何将它们放在一起感到困惑。 当你使用-c标志时,即没有链接完成,输入是C ++代码,输出是目标代码。 .o文件因此不会与-c混合,并且编译器会向您发出警告。 来自目标文件的符号不会移动到其他目标文件中。 所有的对象文件应该在最终的链接器调用上,这里不是这种情况,所以链接器(通过g++前端调用)会抱怨丢失的符号。 这里有一个小例子(为清楚起见,明确地调用g++ ): PROG ?= myprog OBJS = worker.o main.o all: $(PROG) .cpp.o: ...
-
该消息意味着您正在传递链接器标志(如告诉链接器拉入库中的-l )给编译器。 这意味着运行root-config --cflags --glibs是生成链接器标志,并且这些标志正在传递给编译器的CXXFLAGS 。 我不知道root-config是什么,但是你应该调查它的命令行,并以不产生链接器标志的方式调用它。 可能会删除--glibs选项。 ETA:如果你打算在那里运行$(shell ...) ,你真的想要使用:=来分配这些标志变量。 它将以任何方式工作,但如果使用=则每次make扩展变量时都会运行she ...
-
使用类模板的Makefile问题:clang警告链接器输入未使用(Makefile issue with Class template: clang warning linker input unused)[2023-05-08]
只有在编译源文件以创建目标文件时,才需要在命令行中使用-c 。 更改 main.x: main.o ticker.o list.o $(CC) $(CFLAGS) -c main.o list.o ticker.o 至 main.x: main.o ticker.o list.o $(CC) $(CFLAGS) -o $@ main.o list.o ticker.o ^^^^^ $@是规则目标的文件名。 有关更多此类变量, ... -
一个Makefile链接器错误(A Makefile linker error)[2023-08-01]
你错了,这是不需要的。 path /libs表示位于文件系统根目录下的libs目录。 就像/bin并不意味着“我当前目录下的bin目录”,所以/libs并不意味着当前目录下的libs目录。 如果你想查看当前目录中的libs文件,只需使用-Llibs或者如果你想更具体一些, -L./libs . -L./libs (目录.是当前目录,所以cd .改变到当前目录,就像cd ..更改到父目录)。 You're wrong that it's not needed. The path /libs means the ... -
$(OBJDIR)/%.o: $(OBJ) $(DEPS)毫无意义。 您说每个目标文件都依赖于它自己(以及所有其他目标文件)。 这就是为什么make抱怨Circular obj/main.o <- obj/main.o dependency dropped. 。 $(OBJ)不应该在那里。 $(CXX) -c -o $@ $< $(CFLAGS)被破坏,因为$<是第一个先决条件,在这种情况下是obj/reader.o 。 这就像你正在尝试将一个目标文件编译成另一个目标文件,这没有任何意义。 另外,你为什么在 ...
-
编译(-c标志)源文件时,不要给出链接标志。 看看这个示例makefile(非常类似于makefile文档) CPP = g++ CPPFLAGS =-Wall -g OBJECTS = main.o net.o PREFIX = /usr/local .SUFFIXES: .cpp .o .cpp.o: $(CPP) $(CPPFLAGS) -c $< .o: $(CPP) $(CPPFLAGS) $^ -o $@ main: $(OBJECTS) main.o: ...
-
使用-c标志仅在没有链接的情况下编译。 默认制定目标是第一个; 你可能希望这是“全部”。 另外,您不应该在命令行上传递头文件。 all: main.o link.o clang++ -g -Wall -Wextra main.o link.o -o prog1 main.o: main.cpp link.h clang++ -g -Wall -Wextra -c main.cpp link.o: link.cpp link.h clang++ -g -Wall -Wextra -c ...
-
“链接器输入文件未使用,因为链接未完成”是什么意思?(What does “linker input file unused because linking not done” mean? (C makefile))[2022-02-18]
发布的makefile中有几个小疏忽。 其中: 库名称仅在链接步骤中使用,而不是在编译步骤中使用 建议使用'wildcard'make运算符来获取源文件列表。 然后使用patterm替换运算符来获取目标文件的列表: 例如: SRC := $(wildcard *.c) OBJ := $(SRC:.c=.o) 当目标(all,clean)不会生成相同名称的文件时,请在make文件的早期插入.PHONY:语句: 类似于: .PHONY : all clean 发布的make文件没有设施来处理关联的头文件,有 ... -
问题与coreplot有关: CorePlot无法在XCode 4.4中构建 。 只需更新coreplot,一切都将顺利编译。 The issue has to do with coreplot: CorePlot failed to build in XCode 4.4. Just update coreplot and everything will compile smoothly.
-
您需要从a.out命令中删除-c : a.out: test.o extra.o gcc $(T) test.o extra.o 或更好: a.out: test.o extra.o gcc $(T) -o $@ test.o extra.o 或者,还是更好: extra: test.o extra.o gcc $(T) -o $@ test.o extra.o 错误消息通常是因为您在命令行上使用-c指定了-lm内容,但是您没有在此处执行此操作。 OTOH,你用-c选项列出目标 ...