我有C++的经验,但最近在工作中只使用python,我很生疏。下面列出了每个文件:
主要.cpp
#include "stack.h"
int main(int argc, char** argv){
return 0;
}
堆栈.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
生成文件
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
因此,当我cd
进入项目目录并键入make
时,我得到:
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
我一直在四处寻找解决方案,但据我所知我的代码是正确的。我不是在寻求有关实际堆栈实现的帮助,而且我意识到这段代码实际上不会对空的 main 执行任何操作,但我似乎无法修复此编译错误。
最佳答案
使用 g++ 编译 C++,而不是 gcc。此外,您不需要编译 header 。
关于C++ 堆栈实现硬件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12462593/