View previous topic :: View next topic |
Author |
Message |
ashmon
Joined: 06 Oct 2007 Posts: 1
|
Posted: Sat Oct 06, 2007 7:28 pm Post subject: Template class error |
|
|
Hi all,
I am trying to implement a linked list in D and here is the code:
In LinkNode.d:
module LinkNode;
public class Link(T)
{
private:
T data;
Link next;
public:
this()
{
}
this(Link nextNode)
{
next = nextNode;
}
this(T item, Link nextNode)
{
data = item;
next = nextNode;
}
T getElement()
{
return data;
}
Link getNext()
{
return next;
}
void setElement(T nodeItem)
{
data = nodeItem;
}
void setNext(Link nextNode)
{
next = nextNode;
}
}
In LList.d:
module LList;
import LinkNode;
public class LList(T)
{
private:
Link!(T) head;
Link!(T) tail;
Link!(T) curr;
public:
this()
{
}
}
In Driver.d:
import std.stdio;
import std.string;
import LList;
int main()
{
LList!(int) var3 = new LList!(int)();
var3.insert(3);
var3.nextNode();
writefln("%d",var3.currentValue());
return 1;
}
When I compile, I get the following erros:
C:\LinkedList\Driver.d(8: template instance LList is not a template declaration, it is a import
C:\LinkedList\Driver.d(8: Error: LList!(int) is used as a type
C:\LinkedList\Driver.d(8: variable Driver.main.var3 voids have no value
I have been working on this forever and can't find a solution. Any help would be appreciated.
Thanks,
Ashmon |
|
Back to top |
|
|
pqnelson
Joined: 03 Jun 2007 Posts: 13 Location: Davis
|
Posted: Wed Oct 31, 2007 5:56 pm Post subject: |
|
|
Disclaimer: when I compiled this, the GNU D Compiler gave me a number of bugs due to the template usuage, use of an "N" flag, etc. Oddly enough, your bugs went away, so I think it should compile on windoze (which I suspect you are using).
Anyways, here is the modifications I did to your code:
Code: | import std.stdio;
import std.string;
import LList;
int main(char[][] args)
{
/* because you named the module the same thing as the class, you need
* to make certain that you write LList.LList so the compiler doesn't
* bork up!
*/
LList.LList!(int) var3 = new LList.LList!(int)();
var3.insert(3);
writefln("%d",var3.currentValue());
return 1;
} |
Since the code snippet you gave me didn't exactly work, I also had to modify the LList.d file:
Code: | module LList;
import LinkNode;
public class LList(T)
{
private:
Link!(T) head;
Link!(T) tail;
Link!(T) curr;
public:
this()
{
}
// these I added because they were not defined
void insert(T t)
{
Link!(T) tmp = new Link!(T)(t, curr);
curr = tmp;
}
T currentValue()
{
return curr.getElement();
}
} |
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|