Member Avatar for philipghu

Hi everyone,

I just switched from Java to C++ and not so familiar with memory management in C++. I dynamically allocated an array which holds F_HeapNode objects. Whenever I assigned a value to the element of this array, I got an error message saying" no operator = matched these operands".

F_HeapNode *degreeTable;
degreeTable= new F_HeapNode [num];

for (int i = 0; i < num; i++)
degreeTable=NULL;
.....
degreeTable[2]=a;

How do I assign values to the elements of the array like this?

Thanks in advance!

You have to overload operator = in your class.

First of all, don't do this:

for (int i = 0; i < num; i++)
    degreeTable[i]=NULL;

You've already allocated degreeTable to be an array of F_HeapNode instances, meaning that degreeTable is a F_HeapNode instance rather than a pointer to something. If you want an array of pointers, then specify

F_HeapNode ** degreeTable = new F_HeapNode* [num];

but you're better off in C++-land using one of the STL container classes, such as

vector<F_HeapNode> degreeTable(num);

which pre-allocates and fills degreeTable with num default instances of F_HeapNode.

If you are maintaining instances rather than pointers-to-instances (they are distinct in C++, as compared to Java where everything is really a pointer -- or else nothing is, depending on who you ask), then in order to assign a new instance into your container, you need to overload an assignment operator for your class:

F_HeapNode & F_HeapNode::operator=(const F_HeapNode &other)
{
    member1 = other.member1;
    ...
    // any other initialization that makes this instance a copy of the one you're assigning from
    return *this;
}

Then whether you use an array or a std::vector, the assignment above should still work. cplusplus.com is a great reference, including code snippets for almost everything you'd want to do.

Member Avatar for philipghu

Thanks, that's really helpful!

First of all, don't do this:

for (int i = 0; i < num; i++)
    degreeTable[i]=NULL;

You've already allocated degreeTable to be an array of F_HeapNode instances, meaning that degreeTable is a F_HeapNode instance rather than a pointer to something. If you want an array of pointers, then specify

F_HeapNode ** degreeTable = new F_HeapNode* [num];

but you're better off in C++-land using one of the STL container classes, such as

vector<F_HeapNode> degreeTable(num);

which pre-allocates and fills degreeTable with num default instances of F_HeapNode.

If you are maintaining instances rather than pointers-to-instances (they are distinct in C++, as compared to Java where everything is really a pointer -- or else nothing is, depending on who you ask), then in order to assign a new instance into your container, you need to overload an assignment operator for your class:

F_HeapNode & F_HeapNode::operator=(const F_HeapNode &other)
{
    member1 = other.member1;
    ...
    // any other initialization that makes this instance a copy of the one you're assigning from
    return *this;
}

Then whether you use an array or a std::vector, the assignment above should still work. cplusplus.com is a great reference, including code snippets for almost everything you'd want to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.