无效的空指针 - C++ - VS2010

发布于 2024-10-31 07:05:56 字数 1144 浏览 1 评论 0原文

我收到一个断言失败错误,指出表达式:无效的空指针,它指向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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2024-11-07 07:05:56

基于崩溃发生在名为 assign 的函数中的事实,我怀疑 MyClass (直接或间接)包含指针成员,但您尚未定义分配操作符(可能还有复制构造函数和析构函数)正确。事实上,你写

它不允许我创建不是指针的新对象,错误:没有运算符“=”与这些操作数匹配。

如果您尝试像这样创建对象,

MyClass myObject = MyClass(myMap);

则上述编译器错误实际上意味着没有找到合适的赋值运算符来将 MyClass(myMap) 创建的临时对象分配给 myObject。在这种情况下,

MyClass myObject(myMap);

应该编译(尽管它可能会以同样的方式崩溃)。

如果没有看到您正在调用的构造函数的实际代码,就不可能准确判断崩溃发生的原因。

Based on the fact that the crash happens in a function named assign, my suspicion is that MyClass (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 write

it won't let me create new objects that aren't pointers, Error: No operator "=" matches these operands.

If you tried to create your object like this

MyClass myObject = MyClass(myMap);

the above compiler error means indeed that there is no suitable assignment operator found to assign the temporary created by MyClass(myMap) to myObject. In this case,

MyClass myObject(myMap);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文