C++ 堆栈实现硬件错误

标签 c++ makefile compiler-errors stack

我有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/

相关文章:

makefile - makefile 中的命令前面有反斜杠

syntax - PIC18 编译错误 195 是什么意思?

c - IAR编译器警告: "macro does not create a valid token"

android - 按下按钮后应用程式当机,但 Action 正常

c++ - constexpr std::array with static_assert

c++ - 具有持久查找的线性内存容器?

c++ - 仿函数基类的模糊重载

C++ Makefile g++ 和 "std="标志似乎不起作用

c++ - 如何将 Boost 库添加到 RPI2 上的 QTCreator?

c - 包含标题时未定义的函数引用