I'm writing a program that needs to use a "stack" class to store nodes of a map for reference later. I cannot use STL or arrays.
I'm trying to test if this works, it seems to compile, but the code crashes on execution with the error:
"Run-Time Check Failure #3 - The variable 'a' is being used without being defined." on the line "a->cost = 2;".
How can i fix this and what should i do to make this cleaner/ better code.
The rest of my project is to use the stack and an adjacency list to calculate costs of going from one node to the others based one which nodes are linked to which.
Any help would be appreciated, thanks, Aaron.
My code so far:
CODE
//stackP.cpp
//this is my main, testing code here for now.
#include "searchmain.cpp"
using namespace std;
int main(){
stackP *stack = new stackP();
stackP::stackNode *a;
a->cost = 2;
a->L = 'd';
stack->push(a);
stack->dumpstack();
return 0;
}
//stackP.h
#include <iostream>
using namespace std;
class stackP{
public:
stackP();
struct stackNode{
char L;
int cost;
stackNode* below;
};
bool push(stackNode*);
bool pop();
void dumpstack();
private:
stackNode* top;
};
//stackP.cpp
#include <iostream>
#include "stackP.h"
using namespace std;
stackP::stackP()
{
top = NULL;
}
bool stackP::push(stackP::stackNode *node){
stackNode *pushtotop = new stackNode;
top = pushtotop;
pushtotop->cost = node->cost;
pushtotop->L = node->L;
pushtotop->below = top;
return true;
}
bool stackP::pop(){
if(top == NULL){
return false;
}
else{
stackNode *temp;
temp = top;
top = top->below;
delete temp;
}
}
void stackP::dumpstack(){
stackNode *temp = top;
while(temp != NULL){
cout << "cost = " << temp->cost << "airport: " << top->L << endl;
temp = temp->below;
}
}
Thanks, zoto.