在#ifndef处出现标识符错误

时间:2018-07-12 06:12:08

标签: c++

这是我使用堆的优先级队列的实现,但是下面的这段代码中出现错误。错误在#ifndef“ HEAP_H”上显示“ Expected an identifier”

#include "stdafx.h"
#include <iostream>

using namespace std;

#ifndef "HEAP_H"
#define "HEAP_H"


class Heap {

public:
    Heap();

    Heap(int c);

    Heap(const int * Priorities, const int * Elements, int s, int c);

    Heap(const Heap & Heap1, const Heap & Heap2, int c);

    ~Heap();

    // Accessors
    bool empty() const { return hSize == 0; }; // True iff Heap is empty.
    int size() const { return hSize; }; // Current size of Heap.
    int capacity() const { return hCapacity; }; // Current capacity.
    int peekMin() const { return A[0].element; } // Peek at minimum priority element.
    int peekMinPriority() const { return A[0].priority; } // Peek at minimum priority.

                                                          // Modifiers
    void insert(int element, int priority); // Insert the pair <element,priority>.
    int extractMin(); // Remove and return the highest (minimum) priority element.

private:
    class Pair {
    public:
        int element;
        int priority;
    };

    Pair* A; // Array containing heap contents.
    int hCapacity; // Max number of elements (= size of A).
    int hSize; // Current number of elements.

               // Repairs ordering invariant after adding leaf at A[i].
    void trickleUp(int i);

    // Repairs ordering invariant for sub-tree rooted at index i,
    //   when A[i] may be have larger priority than one of its children,
    //   but the subtrees of its children are heaps.
    void trickleDown(int i);

    // Establishes ordering invariant for entire array contents.
    void heapify(); //(Same as "make_heap" in lectures.)

                    // Useful for implementing trickle up and down
    void swap(int i, int j);
};

#endif

我的头文件命名为heap.h,但是在此代码中的#ifndef“ HEAP_H”上出现“期望的标识符”错误。

谢谢。

2 个答案:

答案 0 :(得分:3)

宏是identifiers(不带引号的符号),而不是字符串:

#ifndef HEAP_H
#define HEAP_H

请注意缺少引号。

答案 1 :(得分:1)

您使用#ifdef#ifndef检查是否已定义某些内容。所以应该是

#ifndef HEAP_H
#define HEAP_H