Reputation:
I am trying to delete a class that is nested within another class inside of an array. I get an intellisense error stating:
"expression must be a pointer to a complete object type"
I've tried deleting it another way by creating a pointer to the reference of object and Visual Studio errors out and gives me a dll error basically stating the debugging symbols are not loaded.
Here are pieces of the code --
graphl.cpp
GraphL::~GraphL()
{
NodeData* rmND; // remove NodeData
GraphNode* rmGN; // remove GraphNode
for (int i = 1; i <= size; i++)
{
deleteEdges(adjList[i]->edgeHead);
rmGN = adjList[i];
delete rmGN->data; //////////////// <-- error here
rmGN->edgeHead = nullptr;
delete rmGN;
}
delete[] adjList;
}
graphl.h
class GraphL {
public:
GraphL();
~GraphL();
void buildGraph(istream& infile);
void depthFirstSearch();
//void displayGraph();
private:
struct EdgeNode {
int adjGraphNode;
EdgeNode* nextEdge;
EdgeNode() : adjGraphNode(0), nextEdge(nullptr) {};
};
struct GraphNode {
EdgeNode* edgeHead;
NodeData data;
bool visited;
GraphNode() : edgeHead(nullptr), visited(false) {};
};
static const int MAXNODES = 6;
GraphNode* adjList[MAXNODES]{}; // adjacency list
//void makeEmpty();
int size;
bool insertEdge(int from, int to);
void dfsHelper(int search);
void deleteEdges(EdgeNode* remove);
};
nodedata.h
class NodeData {
friend ostream & operator<<(ostream &, const NodeData &);
public:
NodeData(); // default constructor, data is set to an empty string
~NodeData();
NodeData(const string &); // data is set equal to parameter
NodeData(const NodeData &); // copy constructor
NodeData& operator=(const NodeData &);
// set class data from data file
// returns true if the data is set, false when bad data, i.e., is eof
bool setData(istream&);
bool operator==(const NodeData &) const;
bool operator!=(const NodeData &) const;
bool operator<(const NodeData &) const;
bool operator>(const NodeData &) const;
bool operator<=(const NodeData &) const;
bool operator>=(const NodeData &) const;
private:
string data;
};
If I tried rmND = rmGN->data; delete rmND; Visual Studio gives me a wntdll.pdb not loaded error. How do I properly delete that object? Any help is appreciated! Thank you.
Upvotes: 3
Views: 6088
Reputation: 373
The item you are trying to delete is not a pointer to dynamic memory, it is local to the object and as such, the parent object must be deleted in order for the memory to be cleared.
To be more precise, you can only delete a section of memory which was explicitly created using the new keyword.
It is unnecessary to delete the object.
Upvotes: 4