Windows API 的 STL 成员变量初始化问题

发布于 2024-09-01 21:58:16 字数 350 浏览 2 评论 0原文

我正在创建一个 Windows 应用程序,它使用刺向量作为成员变量。由于某种原因,我可以编译,但是当它尝试获取任何向量成员时就会崩溃。错误为 0xC0000005:读取位置 0xcdcdcdd9 时发生访问冲突。在向量类的成员函数中。

这是 size() 函数崩溃的地方。

size_type capacity() const
{ // return current length of allocated storage
  return (this->_Myend - this->_Myfirst);
}

我正在使用 Visual Studio 2010。

谢谢 姜戈

I am creating a windows app that uses a vector of stings as a member variable. For some reason, I can compile but when it tries to get at any of the vectors members is crashes. the error is 0xC0000005: Access violation reading location 0xcdcdcdd9. in the member function of the vector class.

this is the size() function where it breaks.

size_type capacity() const
{ // return current length of allocated storage
  return (this->_Myend - this->_Myfirst);
}

I am using visual studios 2010.

thank you
Django

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

缪败 2024-09-08 21:58:16

问题不在于 STL 代码,而在于您的代码。

由于您没有粘贴代码,我将向您展示一个应该如何使用字符串向量的示例。

std::vector<std::string> v;
v.push_back("hello");
v.push_back("world!");
//Note the above 2 string literals get implicitly converted to std::string
assert(v.size() == 2);
assert(v[0] == "hello");

The problem is not with the STL code but with your code.

Since you didn't paste your code I'll show you an example of how you should be using a vector of strings.

std::vector<std::string> v;
v.push_back("hello");
v.push_back("world!");
//Note the above 2 string literals get implicitly converted to std::string
assert(v.size() == 2);
assert(v[0] == "hello");
迷雾森÷林ヴ 2024-09-08 21:58:16

我以前也遇到过这样的事情。最可能的原因是堆损坏。

I've come across this kind of thing before. Most likely cause is a corrupt heap.

甜柠檬 2024-09-08 21:58:16

在标题中。

#include <iostream>
#include <string>
#include <vector>

using namespace std;


class Chat
{
protected:
    vector<string> m_buffers;

public:
    Chat();
    ~Chat();
};

在 cpp 中,

Chat::Chat(){
    string init = "test";
    m_buffers.push_back(init); //crash
}
Chat::~Chat(){

}

抱歉缺少代码......我在想什么(手掌)

in the header.

#include <iostream>
#include <string>
#include <vector>

using namespace std;


class Chat
{
protected:
    vector<string> m_buffers;

public:
    Chat();
    ~Chat();
};

in the cpp

Chat::Chat(){
    string init = "test";
    m_buffers.push_back(init); //crash
}
Chat::~Chat(){

}

sorry about the lack of code.... what wa I thinking (face palm)

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