R-value overloaded Operator string assignment error

144 views Asked by At

I am having some trouble figuring out why i get Unhandled exception at 0x003DBD00 in Project10.exe: 0xC0000005: Access violation reading location 0xCDCDCDE5. It seems to pop up at a string assignment operator similar to this: std::string x = std::move(std::string y); Please see code below for more information.

The call starts from here: *members += NVPair<std::string, std::string>(n,v);

FYI members is members = new List <NVPair <std::string, std::string>, DATA_MEMBERS_PER_OBJECT>(); where DATA_MEMBERS_PER_OBJECT is 4

List class declarations:

    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>();

Operator call:

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 Class

//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++;
}

Name Value pair Class

//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; }
};

I have been trying to debug it and it fails and goes into xstring at Name = std::move(add.Name) inside the NVPair =function.

Any help is much appreciated!

Edit: Looks like I was being very vague (Sorry first time). So The main program reads information from a file that contains Name and value pairs. It then creates a List object that is an array of pointers to Name value pair objects. So T * Array[n] is an array of T pointers. The purpose is to store information from a file that looks like this:

{
    "Name" : "Cat"
    "type" : "animal"
}

and so on...

1

There are 1 answers

4
ventsyv On BEST ANSWER

The array member inside your List class is never initialized, but you are referencing it in the += operator. Also, I presume you want an array of NVPairs not array of NVPair pointers

Try doing something along the lines of:

template <typename T>
class List
{
    T Array[n];
    size_t size;
    size_t elements;
    T dummy;

    public:

    List(size_t maxSize) : size(maxSize), elements(0)
    {
       Array = new T[size];
    }

    void operator+=(T&& add)
    { //moves element
       //This was where it was failing because Array was not defined
       Array[elements] = std::move(add);
       elements++;
    }
    //The rest of the class
}

The constructor will create an array of NVPairs, then you assign the values as normal. Notice that you need 2 variables, one for the size, one for the number of elements currently in the array.