[英]Debug Assertion Failure: string subscript out of range
I'm trying to learn coding, and am creating a binary search program. 我正在尝试学习编码,并正在创建一个二进制搜索程序。 When trying to run my code, I get a debug assertion failure (see debug output below) when I try to call upon the search function.
尝试运行我的代码时,当我尝试调用搜索功能时遇到调试断言失败(请参见下面的调试输出)。 What have I screwed up?
我搞砸了什么?
#include<iostream>
#include<string>
using namespace std;
const int N = 100;
string names[N];
string query;
//function to search for names in the list
string search(int largestNum, int smallestNum, string names, string query);
int main(){
cout << "Please enter the names, pressing enter after each entry." << endl
<< "Type done when you are finished the list" << endl
<< "1.";
cin >> names[0];
int ticker1 = 1;
/*this loop outputs a number 1 greater then the last and allows the
user to enter a new name until they type "done" which will exit the loop*/
do {
cout << endl << ticker1 + 1 << ".";
cin >> names[ticker1];
ticker1++;
} while ((names[ticker1 - 1] != "done") && (names[ticker1 - 1] != "Done"));
cout << endl;
int ticker2 = 0;
while (ticker2 < (ticker1 - 1)){
cout << ticker2 + 1 << "." << names[ticker2] << endl;
ticker2++;
}
int largestNum = ticker1;
int smallestNum = 0;
cout << "Please Enter the name that you would like to search for:";
cin >> query;
cout << search(largestNum, smallestNum, names[N], query);
return 0;
}
//function to search for names in the list
string search(int largestNum, int smallestNum, string names, string query){
string middleName;
int middle = ((largestNum - smallestNum) / 2);
middleName = names[middle];
cout << middleName;
if (query == middleName) return { "Present", "Location:" + middle };
else return { "Not around" };
}
DEBUG OUTPUT 调试输出
'Learning C++.exe' (Win32): Loaded 'C:\Users\Gareth\Programming\Projects\Learning C++\Debug\Learning C++.exe'. Symbols loaded.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
Debug Assertion Failed!
Program: C:\Windows\SYSTEM32\MSVCP120D.dll
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring
Line: 1685
Expression: string subscript out of range
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\HsSrv.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dsound.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmmbase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\powrprof.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\devobj.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dwmapi.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\clbcatq.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Program Files (x86)\DisplayFusion\Hooks\AppHookx86_45641EBC-D115-4D9A-BB79-9CB07566629C.dll'. Module was built without symbols.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.9200.16658_none_bf1359a245f1cd12\comctl32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. Cannot find or open the PDB file.
The program '[9200] Learning C++.exe' has exited with code 3 (0x3).
It seems you want your function to find a particular name ( query
) in your array of names ( names
). 似乎您希望函数在名称(
names
)数组中找到特定名称( query
)。 But the function's interface is incorrect for that. 但是该函数的接口对此不正确。
Its parameter names
is currently of type string
, which means it's a single string. 它的参数
names
当前为string
类型,这意味着它是单个字符串。 If you want to pass the global variable names
in, change the function to accept a pointer to string
, and make it so that it points to the first element of the array: 如果要传入全局变量
names
,请更改该函数以接受一个指向string
的指针,并使它指向数组的第一个元素:
string search(int largestNum, int smallestNum, const string *names, string query)
{
// as before..., except:
if (query == middleName) return "Present\nLocation:" + std::to_string(middle);
else return "Not around";
}
At the call site, just pass names
instead of names[N]
(which was accessing an out-of-bounds index anyway): 在呼叫站点,只需传递
names
而不是names[N]
(无论如何,该名称仍在访问越界索引):
cout << search(largestNum, smallestNum, names, query);
The source of the original assertion was that initialising a std::string
with { "Present", "Location: " + middle }
does not do even remotely what you probably thought it does. 原始断言的来源是,使用
{ "Present", "Location: " + middle }
初始化std::string
并不会像您可能认为的那样遥远。 It invokes the std::string
constructor which takes two iterators, expected to delimit a range of characters to copy into the string. 它调用
std::string
构造函数,该构造函数带有两个迭代器,这些迭代器应定界要复制到字符串中的字符范围。
So what you were effectively telling the program: "Start from the address of the first character in the literal "Present"
and go on until you reach the address of the middle
-th character of the literal "Location: "
, copying everything in between into the string. This is of course an absolute mess. 因此,您实际上是在告诉程序:“从文字
"Present"
中的第一个字符的地址开始,一直到到达文字"Location: "
中的middle
字符的地址,然后在两者之间复制所有内容到字符串中,这当然是绝对的混乱。
If you want some learning resources, we keep a list of good C++ books here on Stack Overflow. 如果您需要一些学习资源,我们在Stack Overflow上保留了一些不错的C ++书籍清单 。
You are passing names[N]
to search(..)
, but most likely position 100 does not exist. 您正在将
names[N]
传递给search(..)
,但很可能不存在位置100。 N
is set to 100. Just pass a valid array location or pass the entire array and the length of names
to iterate over names
. N
设置为100。只需传递有效的数组位置或传递整个数组以及names
的长度即可遍历names
。 Also you are using this line middleName = names[middle];
另外,您正在使用此行
middleName = names[middle];
but names
is not a string array, just a string
. 但是
names
不是字符串数组,而只是string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.