Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing ';' before 'template<'

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

like image 385
Howdy_McGee Avatar asked Dec 15 '22 11:12

Howdy_McGee


1 Answers

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()
{
}
like image 150
Drew Dormann Avatar answered Dec 28 '22 07:12

Drew Dormann