I can't seem to figure out why my program crashes every time I do anything to my Node object. And I can't make any progress until this is working...
My problem is: Whenever I try to "Set Data" on the Node X, I get an access violation error. Before that I was getting a Runtime failure #3 about not initializing the variable. So I initialize it to null, and I still get an error. My previous Node classes never gave me this error, so I am kinda stumped. Any help is appreciated!
Here's the code:
Node Class:
#ifndef NODE
#define NODE
template <typename T>
class Node
{
public:
Node();
Node(T data);
void SetData(T data);
private:
T m_Data;
};
template <typename T>
Node<T>::Node()
{
}
template <typename T>
void Node<T>::SetData(T data)
{
m_Data = data;
}
#endif
Main:
#include <iostream>
#include <crtdbg.h>
#include "Node.h"
#define _CRTDBG_MAP_ALLOC
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
Node<int> * x = nullptr;
x->SetData(20);
return 0;
}
The weird thing is, whenever I do a "New" to allocate a new node, this problem doesn't occur.. Thanks in advance.