I have experience in C++ but have recently been using python exclusively at work, and I am very rusty. Each file is listed below:
main.cpp
#include "stack.h"
int main(int argc, char** argv){
return 0;
}
stack.h
#ifndef STACK_H
#define STACK_H
#define NULL 0
template <class elementType>
class stack{
struct node
{
elementType data;
node* next;
};
node* top;
public:
stack(){
top = NULL;
}
~stack(){
node temp = top;
while (top != NULL){
top = top->next;
delete temp;
}
}
void push(elementType x){
node temp = new node();
temp.data = x;
temp.next = top;
top = temp;
}
elementType pop(){
node temp = top;
top = top->next;
return temp;
}
bool isEmpty(){
return top == NULL;
}
};
#endif //STACK_H
makefile
a.out : main.o stack.o
gcc -o a.out main.o stack.o
main.o : main.cpp stack.h
gcc -O -c main.cpp
stack.o : stack.h
gcc -O -c stack.h
clean :
rm main.o stack.o
So, when I cd in to the project directory and type make i get:
gcc -O -c main.cpp
gcc -O -c stack.h
stack.h:7:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
make: *** [stack.o] Error 1
I’ve been searching around for a solution, but as far as I can tell my code is correct. I’m not looking for help with the actual stack implementation, and I realize this code won’t actually do anything with an empty main, but I can’t seem to fix this compile error.
Use g++ to compile C++, not gcc. Also, you don’t need to compile a header.