无效的空指针 - C++ - VS2010
我收到一个断言失败
错误,指出表达式:无效的空指针
,它指向c:\program files\microsoft Visual Studio 10.0\vc\include\xstring行:930
。
这是我的代码:
void main(void)
{
// fillMap() fills map with data from txtfile and returns map<char,int>
map<char, int> myMap = fillMap("file.txt"); // <- takes a string
MyClass *myObject;
// Code reaches here fine
myObject= new MyClass(myMap); // this line causes the error
// Does not get to here
}
在导致错误的行上,我可以将 constructor() 留空,只说 ptr = new MyClass();
,但仍然会得到相同的错误。无论如何,我认为这与我的构造函数有关。
在 MyClass 中,构造函数定义为 MyClass(char=' ', int=0)
。如果我不给它们值,则会收到 No default constructor
错误。
那么,有人可以告诉我如何在 Visual Studio 2010 中跟踪此错误,或者提供可能的解决方案来解释为什么我会遇到这些问题吗?
PS,它不允许我创建不是指针的新对象,错误:没有运算符“=”与这些操作数匹配
。
感谢您的任何帮助。
编辑:如果有帮助,这里是 xstring 的第 930 行:
_Myt& assign(const _Elem *_Ptr)
{ // assign [_Ptr, <null>)
_DEBUG_POINTER(_Ptr); //930
return (assign(_Ptr, _Traits::length(_Ptr)));
}
I am getting an Assertion failed
error stating Expression: invalid null pointer
and it points to c:\program files\microsoft visual studio 10.0\vc\include\xstring line:930
.
Here is my code:
void main(void)
{
// fillMap() fills map with data from txtfile and returns map<char,int>
map<char, int> myMap = fillMap("file.txt"); // <- takes a string
MyClass *myObject;
// Code reaches here fine
myObject= new MyClass(myMap); // this line causes the error
// Does not get to here
}
On the line that causes the errors, I can leave the constructor() blank and just say ptr = new MyClass();
and still get the same error. I think its something to do with my constructor anyway.
In MyClass, the constructor definition is MyClass(char=' ', int=0)
. If I don't give them values, then I get a No default constructor
error.
So, can someone tell me how I can trace this error in Visual Studio 2010, or provide a possible solution as to why I'm getting these problems?
PS, it won't let me create new objects that aren't pointers, Error: No operator "=" matches these operands
.
Thanks for any help.
EDIT: If it helps, here's line 930 from xstring:
_Myt& assign(const _Elem *_Ptr)
{ // assign [_Ptr, <null>)
_DEBUG_POINTER(_Ptr); //930
return (assign(_Ptr, _Traits::length(_Ptr)));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于崩溃发生在名为
assign
的函数中的事实,我怀疑MyClass
(直接或间接)包含指针成员,但您尚未定义分配操作符(可能还有复制构造函数和析构函数)正确。事实上,你写如果您尝试像这样创建对象,
则上述编译器错误实际上意味着没有找到合适的赋值运算符来将
MyClass(myMap)
创建的临时对象分配给myObject
。在这种情况下,应该编译(尽管它可能会以同样的方式崩溃)。
如果没有看到您正在调用的构造函数的实际代码,就不可能准确判断崩溃发生的原因。
Based on the fact that the crash happens in a function named
assign
, my suspicion is thatMyClass
(directly or indirectly) contains a pointer member but you haven't defined the assignment operator (and possibly neither the copy constructor and the destructor) properly. In fact, you writeIf you tried to create your object like this
the above compiler error means indeed that there is no suitable assignment operator found to assign the temporary created by
MyClass(myMap)
tomyObject
. In this case,should compile (although it would probably crash the same way nevertheless).
Without seeing the actual code of the constructor you are calling, it is not possible to tell exactly why the crash happens.