Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template class throwing error C2143: syntax error : missing ';' before '<'

I have 1 templated class split across 2 files that im trying to get to work but cant seem to figure out the solution to this error: error C2143: syntax error : missing ';' before '<'.

The TreeNode.h file is as follows

#ifndef TREENODE_H
#define TREENODE_H

#include <iostream>
#include <vector>
#include <utility>

using namespace std;

template <typename T>
class TreeNode {
public:
    TreeNode();
    ~TreeNode();

    void addChild(TreeNode<T> *newNode);
    void addKey(T& newKey);

    void setIsLeaf(bool value);
    bool isLeaf() { return leaf; }
private:
    vector<TreeNode<T>*> children;
    vector<T> keys;
    bool leaf;
};

#include "TreeNode.tem"

#endif

And here is the "TreeNode.tem" file:

#include <iostream>
#include <utility>

using namespace std;

template<typename T> 
TreeNode<T>::TreeNode()
{
    leaf = true;
}

template<typename T>
TreeNode<T>::~TreeNode()
{
    for (int i = 0; i < children.size(); i++)
    {
        delete children[i];
    }
}

template<typename T>
void TreeNode<T>::addChild(TreeNode<T> *newNode)
{
    children.push_back(newNode);
}

template<typename T>
void TreeNode<T>::addKey(T& newKey)
{
    keys.push_back(newKey);
}

template<typename T>
void TreeNode<T>::setIsLeaf(bool value)
{
    leaf = value;
}

I location of the error is at the first line of the TreeNode constructor implementation.

I did some prior reading and most people who have asked about this error already ended up just having spelling mistakes, and i cant find anything like this in my code. Any ideas on how i could fix this would be great, Thanks in advance, Will.

EDIT:

After some back and forth with DanielFrey we have discovered the cause of this error. When creating the TreeNode.tem file I used the inbuilt Visual Studio file creator (simply "add file") to make a blank *.cpp file which i then renamed to suit my purpose. To fix this I instead created a blank *.h file (as obviously there are metadata difference's) which was then renamed to TreeNode.tem and populated appropriately. This was enough to solve the problem for others having the same problem.

like image 448
Will Bagley Avatar asked Sep 21 '26 10:09

Will Bagley


1 Answers

The code you have shown is OK, it is not the problem in itself. The only thing I can imagine that would lead to this problem is that you (or your build system) tried to compile TreeNode.tem on its own. That can't work and it is not needed anyways.

Make sure you run a test by having this in a separate file:

#include "TreeNode.h"

int main()
{}

and see if it compiles.

like image 155
Daniel Frey Avatar answered Sep 24 '26 02:09

Daniel Frey