失踪 ';' before'template<' [英] Missing ';' before 'template<'

查看:211
本文介绍了失踪 ';' before'template<'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



错误1错误C2143:语法错误: ;'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

解决方案

这个错误可能有点误导。



; 模板之前发生不一定重要。



; 之后<










$ b

档案 header.h

  class MyClass 
{

}

档案 heap.h

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

#endif

文件 main.cpp

  #includeheader.h
#includeheap.h

int main()
{
}

strong>编辑



编译器错误导致错误的文件的原因是编译之前,预处理器将处理 main.cpp 添加到此单一字符流中。

  class MyClass 
{

}

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

int main()
{
}


So I'm getting a strange error when I'm compiling my program:

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

I'm doing everything pretty standard; nothing out of the ordinary:

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

Not quite sure what's wrong. I tried closing and reopening my program - no luck. Using Visual Studio 2010

解决方案

That error can be a little misleading.

It's not necessarily important that a ; occur before template<.

The ; was actually expected after whatever did occur before template<.

This example shows how this could happen.

File header.h

class MyClass
{

}

File heap.h

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

#endif

File main.cpp

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

int main()
{
}

Edit:

The reason this compiler error led you to the wrong file is that before compilation, the preprocessor will process main.cpp into this single stream of characters.

class MyClass
{

}

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

int main()
{
}

这篇关于失踪 ';' before'template&lt;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆