【发布时间】:2014-11-19 20:05:31
【问题描述】:
我在弄清楚为什么我得到Unhandled exception at 0x003DBD00 in Project10.exe: 0xC0000005: Access violation reading location 0xCDCDCDE5. 时遇到了一些麻烦,它似乎在类似于以下内容的字符串赋值运算符处弹出:std::string x = std::move(std::string y); 请参阅下面的代码以获取更多信息。
通话从这里开始:*members += NVPair<std::string, std::string>(n,v);
仅供参考的成员是members = new List <NVPair <std::string, std::string>, DATA_MEMBERS_PER_OBJECT>();,其中DATA_MEMBERS_PER_OBJECT 是4
列出类声明:
auto list = new List <T, OBJECTS_PER_JSON_FILE>(); //Not used in this process
auto members = new List <NVPair <std::string, std::string>, DATA_MEMBERS_PER_OBJECT>();
操作员调用:
n = trim(trim(getName(line)),'"'); //Trim removes extra characters from line and returns Name
v = trim(trim(getValue(line)),'"'); //or Value as a std::string
*members += NVPair<std::string, std::string>(n,v);
列表类
//List.h
#include <iostream>
#include <string>
template <typename T, unsigned int n>
class List{
T *Array[n];
size_t elements;
T dummy;
public:
List(){ elements = 0u; }
size_t size() const { return elements; }
const T& operator[](unsigned int i) const{...}
void operator+=(const T& add){ //adds a copy to the element
*Array[elements] = add;
elements++;
}
void operator+=(T&& add){ //moves element
*Array[elements] = std::move(add);
elements++;
}
名称值对类
//NVPair.h
#include <iostream>
#include <string>
template <typename T, typename B>
class NVPair{
T Name;
B Value;
public:
NVPair(){ Name = ""; Value = ""; }
NVPair(T n, B v){ Name = n; Value = v; }
T name() const { return Name; }
B value() const{ return Value; }
NVPair& operator=(const NVPair& add){
Name = add.Name;
Value = add.Value;
return *this; }
NVPair& operator=( NVPair&& add){
Name = std::move(add.Name);
Value = std::move(add.Value);
return *this; }
};
我一直在尝试调试它,但它失败并进入 NVPair =function 内 Name = std::move(add.Name) 的 xstring。
非常感谢任何帮助!
编辑:看起来我很含糊(对不起第一次)。所以主程序从包含名称和值对的文件中读取信息。然后它创建一个 List 对象,该对象是一个指向名称值对对象的指针数组。所以T * Array[n] 是一个 T 指针数组。目的是存储文件中的信息,如下所示:
{
"Name" : "Cat"
"type" : "animal"
}
等等……
【问题讨论】:
-
T *Array[n];看起来很可疑,就像您提供的其他代码一样。这些东西到底是什么? -
老实说,您看起来好像在取消引用未初始化的指针。
-
信息不足。请发表:1. 列表的声明 2. List 类的构造函数 3. 导致错误的赋值语句。