3

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.

Will Bagley
  • 165
  • 3
  • 13
  • works with gcc and clang. – Adam Oct 12 '13 at 07:29
  • couple unrelated comments: Delete the first three lines from TreeNode.tem. They're duplicates. Also, call it TreeNode.cpp. – Adam Oct 12 '13 at 07:30
  • @Adam NO!!! This is [not going to work as a separate compile unit, it's a template](http://stackoverflow.com/questions/495021)! – Daniel Frey Oct 12 '13 at 07:31
  • @DanielFrey I didn't say to use it as a separate compile unit. Besides, it wouldn't matter because it would be the same as an empty file. – Adam Oct 12 '13 at 07:32
  • @Adam `.cpp` suggests a compile unit to most people and most build systems. Usually this kind of files is called `.ipp` or `.tpp`. If using the `.tem` file as a separate compile unit, your suggestion to rename it to `.cpp` only makes things worse for OP to understand. – Daniel Frey Oct 12 '13 at 07:33
  • @Adam in relation to the renaming i was told that for template's i shouldnt be using .cpp as it implies a sort of file that creates an object. Not sure on the specifics but that shouldn't make much of a difference anyway right? – Will Bagley Oct 12 '13 at 07:39
  • 1
    @WillBagley Read the link in my comment above. It makes a **huge** difference! That said, when we speak about a "separate compile unit", this is what you probably mean by "creates an object" (object file). You can not create an object file from your `.tem` and you don't have to. Make sure you haven't added it in your project the wrong way so VC will not attempt to create an object from it! – Daniel Frey Oct 12 '13 at 07:42
  • @DanielFrey If im not mistaken im using the "common approach" here but with a .tem file rather than the .tpp file. However that does clear up why i am doing so thanks for that. – Will Bagley Oct 12 '13 at 07:45
  • I didn't say to remove the #include! I'd rather have `make` waste a few precious milliseconds doing a no-op than to have files with ambiguous extensions around. `.tpp` is better because *some* people have heard of it, but even that is a small number. – Adam Oct 12 '13 at 07:46
  • OSX has never heard of a `.tpp` either. That just makes it a pain to work with. – Adam Oct 12 '13 at 07:50

1 Answers1

1

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.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • I did that and it does compile with both gcc and clang. – Adam Oct 12 '13 at 07:31
  • @Adam What are you trying to tell me by that? The OP should do this to verify his own environment and understanding of the possible cause for his problem. – Daniel Frey Oct 12 '13 at 07:36
  • I just tried that (using VS2010 at the moment) and still have the same errors. Maybe i messed up my file structuring some how along the way, I'll try to recreate the problem by isolating it. – Will Bagley Oct 12 '13 at 07:36
  • 1
    @DanielFrey I said it to serve as a datapoint. Both to save others effort, and to tell OP that the problem probably isn't in what he posted. – Adam Oct 12 '13 at 07:38
  • @DanielFrey Just deleted and readded all of the files in question (my main test file, the .h and the .tem) and all errors still persist. Not sure where to go from here. – Will Bagley Oct 12 '13 at 07:48
  • @WillBagley Next test: Remove the `.tem` file and add its content to the header. If that works, it at least confirms that I was on the right track. – Daniel Frey Oct 12 '13 at 07:52
  • @DanielFrey As said I literally replaced the #include "...tem" with the code from the file. Now there are no more errors which is great but is there any way that i can have my code structured in the 2-file manner? I have done it before and had no trouble but my old code was no help in sorting out this error, ive never had this one before. Thanks – Will Bagley Oct 12 '13 at 07:59
  • @WillBagley This confirms the problem. In your project **VS is trying to compile the `.tem` file** on its own. You need to make sure this does not happen, but I don't have VS. I suggest you try to rename it to `.ipp` or, when you add it to the project, check what *type* of file VS makes of it. It should be called "header" and not "source" or something. As a **work-around** you might also simply add `#include "TreeNode.h"` at the top of the `.tem` file. That will probably work although you will end up with a superfluous object file. – Daniel Frey Oct 12 '13 at 08:04
  • @DanielFrey Your first suggestion worked (in a round-about way)! The issue was that when you create a file using the VS creator it obviously encodes it with some sort of metadata. I was creating .cpp files and then renaming them to .tem, but they were still being treated as .cpp files. By deleting the original treenode.tem file and creating it as a header file (then renaming it) the 2-file method works perfectly. Thank you very much, ill mark your answer and the correct one and update my post with a sort of summary. – Will Bagley Oct 12 '13 at 08:16