失踪 ';' 在'模板'之前

How*_*Gee 3 c++ templates header-files visual-studio-2010

所以当我编译程序时,我收到一个奇怪的错误:

Error 1 error C2143: syntax error : missing ';' before ''template<''

我做的每件事都很标准; 没什么特别的:

#ifndef HEAP_H
#define HEAP_H
//**************************************************************************
template<typename TYPE>
class Heap
{
    private:
        TYPE* heapData;
        int currSize;
        int capacity;
        void _siftUp(int);
        void _siftDown(int);
        int _leftChildOf(int) const;
        int _parentOf(int) const;

    public:
        Heap(int c = 100);
        ~Heap();
        bool viewMax(TYPE&) const;
        int getCapacity() const;
        int getCurrSize() const;
        bool insert(const TYPE&);
        bool remove(TYPE&);
};
Run Code Online (Sandbox Code Playgroud)

不太确定是什么问题.我尝试关闭并重新打开我的程序 - 没有运气.使用Visual Studio 2010

Dre*_*ann 11

这个错误可能有点误导.

之前;发生的事情并不一定重要. template<

;实际预期后,无论发生之前template<.

此示例显示了这可能发生的方式.

文件 header.h

class MyClass
{

}
Run Code Online (Sandbox Code Playgroud)

文件 heap.h

#ifndef HEAP_H
#define HEAP_H
//**************************************************************************
template<typename TYPE>
class Heap
{
};

#endif
Run Code Online (Sandbox Code Playgroud)

文件 main.cpp

#include "header.h"
#include "heap.h"

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

编辑:

此编译器错误导致您返回错误文件的原因是编译之前,预处理器将处理main.cpp为此单个字符流.

class MyClass
{

}

//**************************************************************************
template<typename TYPE>
class Heap
{
};

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

  • @WouterHuysentruit,这是最合乎逻辑的,真的. (4认同)
  • @WouterHuysentruit也许是一个猜测,但我敢打赌一些切达干酪是正确的. (3认同)
  • @Howdy_McGee原因是预编译器在编译器产生该错误之前删除了*单个文件*的概念. (2认同)