变量未在此范围内声明数组线性搜索 我试图用C++来编写一个程序,它将使用一个单独的搜索函数在一个大小为10的数组中搜索期望的值。代码如下:
main.cpp变量未在此范围内声明数组线性搜索 我试图用C++来编写一个程序,它将使用一个单独的搜索函数在一个大小为10的数组中搜索期望的值。代码如下:,c++,algorithm,linear-search,C++,Algorithm,Linear Search,main.cpp #include <iostream> #include <array> using namespace std; int main() { cout << "Welcome to the array linked list program."; int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int d = 0; cin >&
#include <iostream>
#include <array>
using namespace std;
int main()
{
cout << "Welcome to the array linked list program.";
int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int d = 0;
cin >> d;
while (d =! 0)
{
cout << "Number to be found";
cin >> d;
bool found = seqSearch1(sanadA, 10, d, -1);
cout << found;
}
}
#包括
#包括
使用名称空间std;
int main()
{
cout>d;
而(d=!0)
{
cout>d;
bool-found=seqSearch1(sanadA,10,d,-1);
cout我假设错误发生在这一行:
bool found = seqSearch1(sanadA, 10, d, -1);
问题是您没有声明任何名为seqSearch1()
的函数。相反,您有一个名为jw\u search()
的函数。因此,您可以将该行更改为:
bool found = jw_search(sanadA, 10, d, -1);
但您还需要一个名为seqSearch1.h
的头文件,其中包含以下行:
bool jw_search (int *list, int size, int key, int*& rec);
最后将这一行添加到main.cpp
的顶部:
#include "seqSearch1.h"
编译代码时,需要在命令中包含所有源文件。例如,如果使用的是g++
,则可以执行以下操作:
g++ main.cpp seqSearch1.cpp
要了解其工作原理,您需要了解头文件以及函数声明和函数定义之间的区别。您还应该了解编译器和链接器之间的区别。Code Peedient可以直接回答您的问题。如果您希望代码包含在多个文件中,那么seqSearch1函数需要是main.cpp或通过#include指令包含
代码有多个问题。我已经为您解决了一些问题,并将其放在一个文件中
#include <iostream>
#include <array>
using namespace std;
bool seqSearch1 (int *list, int size, int key, int& rec)
{//Basic sequential search.
bool found = false;
int i;
for(i=0;i<size;i++)
{
if (key == list[i])
{
found = true;
rec = i;
break;
}
}
return found;
}
int main()
{
cout << "Welcome to the array linked list program." << endl;
int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int d = -1;
while (d != 0)
{
cout << "Number to be found, 0 to end?";
cin >> d;
if(d == 0) break;
int index = -1;
bool found = seqSearch1(sanadA, 10, d, index);
if(found) cout << "Found" << endl;
else cout << "Not Found" << endl;
}
}
#包括
#包括
使用名称空间std;
bool seqSearch1(int*列表、int大小、int键、int&rec)
{//基本顺序搜索。
bool-found=false;
int i;
对于(i=0;In行引起错误?<代码> d=0?<代码> >?您是否指在数组中搜索期望值?”-C++具有为此目的的内置函数,例如:<代码>布尔OjjWySt搜索(int * list,int大小,int key,int *+cR){int *= list +sig;Rec= STD:::查找(列表,结束,key);返回(Rec.OnEd);};
OP将第二个代码块标记为seqSearch1.cpp
,但代码在任何地方都没有显示。他可能在名称上犯了错误,或者可能是故意漏掉了。@LAD“但代码在任何地方都没有显示”代码没有显示什么?OP帖子中的第二个代码块没有任何关于seqSearch1.cpp
的内容,尽管代码被标记为seqSearch1.cpp
。我是说OP可能在名称上犯了错误,或者只是遗漏了一些代码。对不起,如果我解释得不好的话。+1是好答案,though.@LAD正如你所说,OP清楚地将一段代码标记为seqSearch1.cpp
。但是,它们没有清楚地显示用于编译的命令,所以我添加了关于如何正确执行的更多细节。代码还有一些其他问题,但我不打算花太多时间深入研究。很酷,只是想poi请不要把它说出来(尽管有点困惑),这样也许会有帮助。谢谢你的反馈。
#include <iostream>
#include <array>
using namespace std;
bool seqSearch1 (int *list, int size, int key, int& rec)
{//Basic sequential search.
bool found = false;
int i;
for(i=0;i<size;i++)
{
if (key == list[i])
{
found = true;
rec = i;
break;
}
}
return found;
}
int main()
{
cout << "Welcome to the array linked list program." << endl;
int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int d = -1;
while (d != 0)
{
cout << "Number to be found, 0 to end?";
cin >> d;
if(d == 0) break;
int index = -1;
bool found = seqSearch1(sanadA, 10, d, index);
if(found) cout << "Found" << endl;
else cout << "Not Found" << endl;
}
}