【发布时间】:2018-07-29 09:13:15
【问题描述】:
我不断收到此错误。我知道是什么功能导致它,但不知道如何修复它。从这个post在线查找说:
您需要传递一个指向动态分配对象的指针,或者在您的chainLink 类中创建自己的。
但是,当我尝试传递一个字符串指针时。错误仍然弹出。这是我的代码。
#include <iostream>
#include "MWTNode.h"
#include "MWT.h"
using namespace std;
int main() {
MWT t;
string str ="abc";
string* strPtr = &str;
t.insert(strPtr);
std::cout << "Hello, World!" << std::endl;
return 0;
}
#include "MWTNode.h"
class MWT {
public:
MWTNode *root;
string find(const string &);
void insert(const string* string);
};
void MWT::insert(const string* word) {
MWTNode* curr = root;
MWTNode newNode;
string w = *word;
for (int i = 0; i < word->length(); i++) {
const char c = w[i];
if (curr->children.find(c) == curr->children.end()){
//curr->children[c]= MWTNode();
//node->frequency=node->frequency+1;
}
curr = &(curr->children[c]);
}
curr->flag = true;
}
#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
#include <set>
using namespace std;
class MWTNode {
public:
unordered_map<char, MWTNode> children;
string value;
bool flag;
int frequency;
MWTNode(const string &);
MWTNode(const char c);
MWTNode();
void setFrequency ();
int getFrequency ();
};
MWTNode::MWTNode(const string &val) {
value = val;
flag = false;
frequency = 0;
}
MWTNode::MWTNode(const char c) {
value =c;
flag = false;
frequency = 0;
}
MWTNode::MWTNode() {
value ="";
flag = false;
frequency = 0;
}
【问题讨论】:
-
指针是无用的,因为你复制它指向的字符串并且不再使用指针。
-
剪掉闲聊的素材后,你的标题是“C++ 指针被释放没有被分配 *** 在 malloc_error_break 中设置断点来调试”。这是一条错误消息吗?哪里来的?
标签: c++11 pointers reference pass-by-reference pass-by-pointer