c++ - 在 ';' 之前缺少 'template<'

标签 c++ visual-studio-2010 templates header-files

我在编译我的程序时遇到了一个奇怪的错误:

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&);
};

不太确定哪里出了问题。我尝试关闭并重新打开我的程序 - 没有运气。使用 Visual Studio 2010

最佳答案

这个错误可能有点误导。

; 不一定重要发生在之前 template< .

;template< 之前发生的实际上是预期的之后 .

这个例子展示了这是如何发生的。

文件 header.h

class MyClass
{

}

文件 heap.h

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

#endif

文件 main.cpp

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

int main()
{
}

编辑:

此编译器错误导致您找到错误文件的原因是 编译之前,预处理器将处理main.cpp。进入这个单一的字符流。

class MyClass
{

}

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

int main()
{
}

关于c++ - 在 ';' 之前缺少 'template<',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092520/

相关文章:

c++ - 在 C++ 中使用 QStringList 构建 QHash

c++ - 我可以在 C++ 中创建强类型整数吗?

asp.net - 元素 'system.webServer' 具有无效的子元素 'rewrite' 。我应该解决这个问题吗?如何解决?

templates - 将 Handlebars 组件输出作为参数传递给助手

templates - 在 go html 模板中创建循环的最佳方法是什么?

c++ - 在不同的内存位置同时写入 std::deque 是线程安全的吗?

c++ - 来自模板类的友元函数

php - Web服务和ORM框架?

winforms - 在VS2010中打开窗体时,TFS自动 check out Windows窗体

C++ 模板函数检查 vector 是否包含该值?