Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试使用宽字符串创建类时发生访问冲突_C++_Class_Crash_Access Violation_Widestring - Fatal编程技术网

C++ 尝试使用宽字符串创建类时发生访问冲突

C++ 尝试使用宽字符串创建类时发生访问冲突,c++,class,crash,access-violation,widestring,C++,Class,Crash,Access Violation,Widestring,我正在编写一个代码,用来读取调试日志,解析日志,并激活一个google文本转换语音,它将提供关于吐出它的游戏的信息 日志采用UTF-8编码,因此我在代码中使用宽字符串,代码编译和解析字符串都很好,直到我尝试创建类对象为止 然后我得到一个访问冲突: 'CMakeProject1.exe' (Win32): Loaded 'C:\Users\OptoCloud\CMakeBuilds\e36ef523-902d-0932-93cd-2201bbe1e731\build\x64-Release\CMa

我正在编写一个代码,用来读取调试日志,解析日志,并激活一个google文本转换语音,它将提供关于吐出它的游戏的信息

日志采用UTF-8编码,因此我在代码中使用宽字符串,代码编译和解析字符串都很好,直到我尝试创建类对象为止

然后我得到一个访问冲突:

'CMakeProject1.exe' (Win32): Loaded 'C:\Users\OptoCloud\CMakeBuilds\e36ef523-902d-0932-93cd-2201bbe1e731\build\x64-Release\CMakeProject1\CMakeProject1.exe'. Symbols loaded.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Program Files\AVAST Software\Avast\x64\aswhooka.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbase.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\msvcp140.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140.dll'. Cannot find or open the PDB file.
The thread 0x4174 has exited with code 0 (0x0).
Exception thrown at 0x00007FF7FBDA92B3 in CMakeProject1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
std::wstring线;
std::wstring传递了\u字符串;
std::string path=“c:\\Users\\OptoCloud\\AppData\\LocalLow\\Game\\output\u log.txt”;
std::wifstream文件(路径,std::ios::binary);
类创建对象
{
公众:
创建_对象();
创建_对象(std::wstring传递的_字符串);
std::wstring Object_string=0;
};
Create_Object::Create_Object(){}
创建\u对象::创建\u对象(std::wstring传递的\u字符串)
{
Object_string.reserve(传递的_string.length());
对象\u字符串=传递的\u字符串;
}
映射对象列表;
int main()
{
while(true)
{
file.open(path.data());
if(file.is_open())
{
while(std::getline(文件,行))
{
if(line.find(L“DELIMITER1”)!=std::string::npos)
{
传递的字符串=line.substr(line.find(L“DELIMITER1”)、line.find(L“DELIMITER2”);
objectlist[passed\u string]=Create\u对象(passed\u string);//碰撞点////////////////////////////////
}
file.close();
打破
}
}
}
返回1;
}

(已编辑)

代码的问题是,在您的
Create\u对象
类中,正在使用0值构造
std::wstring
成员:

std::wstring Object_string = 0;
使用0构造
std::basic_字符串(std::wstring
基于该字符串)是未定义的行为。发生的情况是0隐式转换为字符指针,因此将调用此构造函数:

std::wstring(const wchar\u t*p)

此构造函数假定传入的指针不是null,并且指向以null结尾的字符串。由于将访问空指针,因此会出现访问冲突错误

解决此问题的方法是取消该初始化:

std::wstring Object_string;
为了进一步证明这个错误,这里有一个例子。
正在删除初始化。

使用调试器查看错误发生的位置。我们需要一个帮助来帮助调试完成,但我没有从中获得任何有用的信息。@OptoCloud
std::wstring Object\u string=0--这应该做什么?
std::wstring
不能为0——您没有处理字符指针,因此将
std::wstring
设置为0是没有意义的。它具有所有未定义行为的特征。去掉
=0
并查看您的问题是否神奇地消失。@OptoCloud。这个问题正是我所指出的。不存在字符串为0的情况。或者至少,这是多个问题中的一个——但是您看到代码是一个,您应该提前提供的东西。请注意,任何
std::basic_string
都会发生同样的可怕情况。是的,您是对的。任何
basic\u字符串
都会出错。我会在我把剩下的代码修复好后再看看是否能修复,谢谢
std::wstring Object_string;