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
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
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;
Related
Greetings and thank you in advance!
I'm working in macOS X 10.12; Eclipse Neon 4.6, Compiling using macOS X GCC. I am receiving the following error:
../matrix.h:82:1: error: 'Matx' is not a class, namespace, or enumeration
`Matx::~matx(){`
`^`
`../matrix.h:27:7: note: 'Matx' declared here`
The error is confusing due to the following matrix.h file:
#ifndef MATRIX_H_
#define MATRIX_H_
#include <iostream>
template <class T>
class Matx {
int ROWS, COLS ;
int colix[COLS], rowix[ROWS] ;
T ** array ;
Matx(int, int) ;
~Matx() ;
void rowSwap() ;
void size( void ) ;
void swapRows(int i1, int i2) { std::swap(this->array[i1], this->array[i2]); }
void printMat( void ) ;
};// end class matrix
template <class T>
Matx::~Matx(){
delete this->array ;
}// end ~matx()
Note there are a few more functions in the file, but the error is consistent across all of them. I have tried defining the functions with scope resolution and without, i.e. Matx::~m but to no avail. Any help is much appreciated!
You should write the definition of the function like this:
template <class T>
Matx<T>::~Matx(){
delete this->array ;
}// end ~matx()
This Part is wrong.
int ROWS, COLS ;
int colix[COLS], rowix[ROWS] ;
your're defining arrays of size COLS and ROWS. But these are non-const member variables. You need compile time expressions. For example:
static constexpr int ROWS = 4;
static constexpr int COLS = 4;
I have posted this already one day ago but I did not know how to add a second question to my first question.
I get a forward declaration error. You told me that it should be no problem if I define my class in KdTree.h and my functions, structs, etc in KdTree.cpp. However it does not work so here I post my whole code:
This is my header:
#Includes <iostream>
#Includes others
using namespace TooN;
#ifndef KDTREE_H_
#define KDTREE_H_
class KdTree {
public:
KdTree(std::vector<TooN::Vector<3,GLfloat> > & ,size_t);
struct node;
struct temptask;
struct temphold;
struct ...;
double function(...);
...;
std::vector<node> nodes;
std::vector < int > searchInRadius(const TooN::Vector<3, GLfloat> &,float , const std::vector<TooN::Vector<3,GLfloat> > & );
};
#endif
So and this is my KdTree.cpp:
#include "KdTree.h"
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
const size_t stacksize = 200;
nodes.push_back(node());
temphold tasksarray[stacksize] = {0,pointssize-1,0,0};
int taskindex = 0;
...A lot more stuff
if (!is_leaf(n)){
do something;
}
}
And then my functions in KdTree.cpp
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
And here my first 3 Compiler messages:( :
jni/Visual/KdTree.cpp: In constructor 'KdTree::KdTree(const std::vector<TooN::Vector<3, float> >&, size_t)':
jni/Visual/KdTree.cpp:32:23: error: invalid use of incomplete type 'struct KdTree::node'
nodes.push_back(node());
^
In file included from jni/Visual/KdTree.cpp:8:0:
jni/Visual/KdTree.h:29:9: error: forward declaration of 'struct KdTree::node'
struct node;
^
jni/Visual/KdTree.cpp:33:54: error: elements of array 'KdTree::temphold tasksarray [200]' have incomplete type
temphold tasksarray[stacksize] = {0,pointssize-1,0,0}; //starting at firstpoint = 0 index, lastpoint = lastindex, nodenumber = 0 index, dim = x-dimension (i.e. 0)
And a lot more of these kind of messages.
The struct definitions must still occur before their first use in the .cpp file.
#include "KdTree.h"
struct KdTree::node{
std::size_t a, b, c;
node() : a(-1), b(-1), c(-1) {}
bool is_leaf(const node &n){
return blablabla;
}
};
KdTree::KdTree(std::vector<TooN::Vector<3,GLfloat> > & points, size_t pointssize){
...A lot more stuff
}
Put the implementation of node at the beginning of KdTree.cpp, or at least before its first use:
#include "KdTree.h"
struct KdTree::node {
// ...
};
KdTree::KdTree() {
// ...
}
I am a beginner in c++ so please excuse me if I my mistakes below turn out to be silly. Still, I am stuck with my code and would appreciate any help.
I get the following error when trying to compile through make via g++:
In file included from A.cpp:2:
List.h:20: error: ‘List’ is not a template type
A.cpp: In member function ‘void A::NowyObiekt(int)’:
A.cpp:6: error: ‘list_a’ was not declared in this scope
make: *** [A.o] Error 1
My code is separated into the following tiny files:
A.h : http://pastebin.com/QQ04xx2j (header)
A.cpp : below
#include "A.h"
#include "List.h"
void A::NewObject(int i)
{
list_a.Add(i);
}
int A::Compare(int a, int b)
{
if ( a>b ) return 1;
if ( a<b ) return -1;
else return 0;
}
List.h : below (header)
#ifndef LIST_H
#define LIST_H
template<typename T>
class Node
{
Node()
{
nxt = pre = 0;
}
Node(const T& el, Node *n = 0, Node *p = 0 )
{
dana = el; nxt = n; pre = p;
}
T dana;
Node *nxt, *pre;
};
template<typename T>
class List
{
public:
List()
{
head = tail = 0;
}
void Add(const T&);
protected:
Node<T> *head,*tail;
};
#endif
List.cpp : http://pastebin.com/a3HQ9yZ4
prog.cpp : below (main)
#include "List.h"
#include "A.h"
int main()
{
int i = 5;
class List list_a;
class A obj;
obj.Add(i);
}
and the makefile is : http://pastebin.com/GTR5jW54
As noted, I am still a beginner, so please be understanding. I would be thankful for any help and clear explanations. Thanks in advance.
There are a couple of problems with your code: The first is that you don't declare any variable named list_a anywhere. That error should be pretty obvious. The other is that you use the List class without giving it template parameters.
And last a small note about your question: As your files are indeed very small, you could put them in the question and not link to them.
Edit: About the List template problem.
You already use Node properly in List, i.e. declare the nodes as Node<T>. When you use List you simply has to do the same. For example, to declare a list of integers:
List<int> my_int_list;
Also, as you only use public functions in List from the class A, you don't need the friend declaration. If you do need to use protected or private members (which IMO is a sign of bad design) you need to make that friend-declaration templated as well:
friend class List<sometype>;
And finally, your code will not compile anyway... The reason being that when you are using a template-class, the whole class has to be fully defined (i.e. complete with its function implementations). You can solve this by putting the functions in the header file. And when defining the functions, you need the template parameter there as well:
template<typename T>
void List<T>::Add(const T& el)
{
Node<T>* head = new Node<T>(el);
if ( Compare(el,i) > i )
std::cout << "Ok" << std::endl;
}
Note that I added the template parameter in a couple of places.
I am pretty new to the concept of templates. Am I missing something?
#ifndef STACK_H
#define STACK_H
template < class T>
class Stack{
private:
struct node {
T data;
node* next;
};
node * top;
//node* getNewNode(T num);
};
//template <class T>
//(node*)Stack<T> :: getNewNode(T num){
// node * temp = new node;
// temp->data = num;
// temp->next = NULL;
// return temp;
// }
#endif
When I uncomment the function getNewNode and commend the corresponding statement to getNewNode function, complier gives error like
Why is my function getNewNode not working as expected. Where did I go wrong?
Error 7 error C2470: 'Stack<T>::getNewNode' : looks like a function
definition, but there is no parameter list; skipping apparent
body c:\users\nitinjose\documents\visual studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Error 2 error C4430: missing type specifier - int assumed. Note: C++
does not support default-int c:\users\nitinjose\documents\visual
studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Error 5 error C2146: syntax error : missing ')' before identifier
'num' c:\users\nitinjose\documents\visual studio
2010\projects\templatequeue\templatequeue\stack.h 26 1 TemplateQueue
Defining a member function outside of a class body changes the rules for what names are accessible at that point slightly. In your case the compiler has no idea what node is. You need to tell him that the node* is actually in the class Stack<T>, e.g. typename Stack<T>::node. The typename is necessary here, because node is a dependent name.
The specification of the return value is wrong. Try this
template <class T>
typename Stack<T>::node* Stack<T> :: getNewNode(T num){
// ...
}
I am trying to make some sort of templated Queue class. It seems ok but I am getting 2 errors in the same line which I can't figure out why. The errors appear in the implementation file .cpp where I am trying to give the definition for the destructor. Here is the code of the header file of the class:
#ifndef QUEUETP_H_INCLUDED
#define QUEUETP_H_INCLUDED
template <class T>
class QueueTp
{
private:
struct Node { T item; struct Node * next;};
enum {QSIZE = 10};
//Queue's head
Node *head;
//Queue's tail
Node *tail;
int size;
int maxsize;
QueueTp(const QueueTp & q);
QueueTp & operator=(const QueueTp & q) { return *this;}
public:
QueueTp(): size(0),head(0),tail(0),maxsize(QSIZE) {};
QueueTp(int q = QSIZE): size(0),head(0),tail(0),maxsize(q) {};
~QueueTp();
bool isEmpty(){return size==0;}
bool isFull() {return size==maxsize;}
int sizecur() {return size;}
bool push(const T& t);
bool pop(T& t);
};
#include "QueueTp.cpp"
#endif // QUEUETP_H_INCLUDED
And here is the definition of the destructor in the implementation file:
#include "QueueTp.h"
#include <iostream>
using namespace std;
typename <class T> //<-<-<- in this line I am getting the two errors
QueueTp<class T>::~QueueTp()
{
Node *ptr;
cout<<endl<<"Deleting the queue...";
while (head !=NULL)
{
ptr = head->next;
delete head;
head = ptr;
}
}
//......other method definitions
The errors are pointed above and the specific error messages I get from the compiler are the ones below.
error: expected nested-name-specifier before ‘<’ token|
error: expected unqualified-id before ‘<’ token|
||=== Build finished: 2 errors, 12 warnings ===||
Please use "template" instead of "typename" on the line where you are getting the two error messages! I find that most of the time, an unidentified keyword or a real keyword in the wrong place often gives errors similar to an undefined type, the next symbol after it would cause an error.