-3

I'm having this problem, where I get weird syntax errors in my skip list implementation and seriously have no clue what could cause this.

This is the code:

skipnode.h:

template <typename T>
class SkipNode
{
public:
    T data;
    SkipNode<T> **next;
    SkipNode(T d, int level);
    ~SkipNode();
};

skipnode.cpp

#include "skipnode.h"
template<typename T>
SkipNode<T>::SkipNode(T d, int level)
{
    data = d;
    next = new SkipNode<T>*[level];

    for (int i = 0; i <= level; i++)
        next[i] = 0;
}

template<typename T>
SkipNode<T>::~SkipNode()
{
    delete [] next;
}

Skiplist.h

#include "skipnode.cpp"
#define MAXLEVEL 4;
template<typename T>
class SkipList
{
public:
    SkipList();
    ~SkipList();
    int randLvl(int max);
    T search(T);
    void insert(T);
private:
    SkipNode<T> *root; 
};

Skiplist.cpp

#include "skiplist.h"
template<typename T>
SkipList<T>::SkipList()
{
    root = new SkipNode<T>(0,MAXLEVEL);
}

When I declare root in Skiplist() I get the following error:

 error C2143: syntax error : missing ')' before ';'

Can anyone help me out? Thanks in advance.

Edit: Fixed code, so show includes

Thisen
  • 183
  • 1
  • 9

2 Answers2

4

The root cause of your problem is here:

#define MAXLEVEL 4;

The semicolon is present in the macro expansion, so after the preprocessor pass you end up with:

root = new SkipNode<T>(0, 4;);

Which is a syntax error (extra semicolon before the closing parenthesis).

To fix it, omit the semicolon in your macro definition:

#define MAXLEVEL 4
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Thanks alot! If I post my whole code, could you maybe help me with this problem? [Link](http://i.imgur.com/EiP8ewN) – Thisen Dec 03 '14 at 14:31
  • @Thisen, that problem is heap corruption, most probably triggered by a buffer overflow. You should ask another question about it, but be sure to investigate with a debugger beforehand. – Frédéric Hamidi Dec 03 '14 at 14:33
  • I can only post every 90 mins, sadly. – Thisen Dec 03 '14 at 14:59
2

You need to include skipnode.h, or at least declare

template <typename T> class SkipNode;

before you can use the name SkipNode in the definition of SkipList.

You'll also (almost certainly) need to define the template member functions in your headers, not source files, as explained here.

You also have a problem with

#define MAXLEVEL 4;

which will expand to 4;, inserting a rogue ; in the middle of an expression. Use a less broken macro

#define MAXLEVEL 4

or, better still, a language-level constant

const int max_level = 4;
Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I'm including "skipnode.cpp" in "skiplist.h". Should that not work? – Thisen Dec 03 '14 at 14:16
  • @Thisen: Yes, although that's rather a confusing way to organise the code, since `.cpp` is usually taken to mean a compilable source file, not a header fragment. I'm not sure what the problem is now you've changed the question. Perhaps you could tell us which line of code causes the error. – Mike Seymour Dec 03 '14 at 14:18