CList's AddTail() assertion error

544 views Asked by At

I was trying to create a double-dimensional List in MFC in order to save and work with some int and CString data. So, I've tried something like this:

#include "A.h"
//A.cpp

A::A()
{
}

A::~A()
{
}

//**********************

#pragma once
// A.h
class A: public CObject
{
public:
    A();
    virtual ~A();

    int ID;
    CString label;

};

//**********************

#include "A.h"
#pragma once

// B.h

class B : public CObject
{
public:
    B();
    virtual ~B();

    int anotherID;
    CString anotherLabel;
    CList<A*, A*&> * AList;
    CList<CString, CString&> * TestList;
};

//Note: B.cpp is pretty much the same as A.cpp

//*********************

//C.cpp

void C::Foo()
{
    B * b = new B;
    A * a = new A;
    a->ID = 1;
    a->label = L"something";
    b->AList->AddTail(a); //Assertion error!
    CString aux = L"another thing";
    b->TestList->AddTail(aux); //Assertion error!
}

Here's the problem: when I try to use the AddList() method, I receive the error "Access violation reading location". I first thought that the problem was related to the CObject derived classes, but I am not sure if this is the real problem. I've also tried to do some new and delete overloading, but the problem became even worst. Any ideas?

1

There are 1 answers

0
Edward Clements On

Both the list elements are declared as pointers, so you'll need to either allocate them or declare them as

CList<A*, A*&> AList;              // without the "*"
CList<CString, CString&> TestList; // without the "*"