我正在尝试用 C++ 编写一个程序,该程序将使用单独的搜索函数在大小为 10 的数组中搜索所需的值。下面是代码:
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 >> d;
while (d =! 0)
{
cout << "Number to be found";
cin >> d;
bool found = seqSearch1(sanadA, 10, d, -1);
cout << found;
}
}
seqSearch1.cpp
#include <iostream>
using namespace std;
bool jw_search (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])
{
break;
}
if (i < size)
{
found = true;
rec = &list[i];
}
}
return found;
}
我收到错误:
C:\Users\tevin\Documents\sanad\main.cpp|13|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|
C:\Users\tevin\Documents\sanad\main.cpp|19|error: 'seqSearch1' was not declared in this scope|
我需要帮助来弄清楚为什么会发生这种情况。
最佳答案
我假设错误发生在这一行:
bool found = seqSearch1(sanadA, 10, d, -1);
问题是您尚未声明任何名为 seqSearch1()
的函数。相反,您有一个名为 jw_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
要了解其工作原理,您需要了解头文件以及函数声明和函数定义之间的区别。您还应该了解编译器和链接器之间的区别。
关于c++ - 在此范围内未声明的变量 数组线性搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51814134/