C++ 读取访问冲突。 _Val 是 nullptr
C++ Read access violation. _Val was nullptr
我正在尝试设置包含在智能指针指向的结构中的变量的值。
对于上下文:
// Represents a node in the search tree.
struct SNode
{
int x; // x coordinate
int y; // y coordinate
}
指针(声明为unique_ptr<SNode>start
)被称为start
。
我正在从文件中读取数据,这是问题行:
inFile >> move(start)->x;
执行时,执行此行,"istream"内出现如下错误:
“抛出未处理的异常:读取访问冲突。
_Val 是 nullptr。"
我对智能指针的概念还很陌生,所以我不太清楚我个人是否在这里做错了什么,所以非常感谢您的帮助。
智能指针包含原始指针,因此它需要指向某物。该错误清楚地表明它没有指向任何内容。您的代码中是否有 new
或 std::make_unique<>
语句?
你需要这样的东西:
unique_ptr<SNode> start(new SNode);
或者:
unique_ptr<SNode>start = std::make_unique<SNode>();
我正在尝试设置包含在智能指针指向的结构中的变量的值。
对于上下文:
// Represents a node in the search tree.
struct SNode
{
int x; // x coordinate
int y; // y coordinate
}
指针(声明为unique_ptr<SNode>start
)被称为start
。
我正在从文件中读取数据,这是问题行:
inFile >> move(start)->x;
执行时,执行此行,"istream"内出现如下错误:
“抛出未处理的异常:读取访问冲突。 _Val 是 nullptr。"
我对智能指针的概念还很陌生,所以我不太清楚我个人是否在这里做错了什么,所以非常感谢您的帮助。
智能指针包含原始指针,因此它需要指向某物。该错误清楚地表明它没有指向任何内容。您的代码中是否有 new
或 std::make_unique<>
语句?
你需要这样的东西:
unique_ptr<SNode> start(new SNode);
或者:
unique_ptr<SNode>start = std::make_unique<SNode>();