| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
June 03, 2014 Fighting compiler - experienced programmer but D novice | ||||
|---|---|---|---|---|
| ||||
./graph_structures.d(124): Error: class graph_structures.node(D,
E) is used as a type
I have no idea what this means:( Once we create a class, the
textbook examples show its use as a type which I believe is what
C++ & Java allow. Here's some code:
class node(D, E) {
int nid;
D data;
E[] in_edges; // All edges for undirected graphs go here.
E[] out_edges; // Only used by directed graphs
this(D, E)(D data, int n) {
this.data = data;
nid = n;
in_edges = new E[];
out_edges = new E[];
}
This is incomplete, but I believe the relevant stuff is my node
template uses 2 parameters corresponding to the data types for
the stored data and the edge types. Here's statement 124 in main:
auto fee = new node(string, u_edge)("Suck Moose", 1);
I pass the 2 data types in the first parm list and the
constructor required arguments in the second list. I must be
missing something, but I don't see it:(
Thanx for any help - Charlie
| ||||
June 03, 2014 Re: Fighting compiler - experienced programmer but D novice | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Charles Parker | On Tue, Jun 03, 2014 at 03:17:09AM +0000, Charles Parker via Digitalmars-d-learn wrote: > ./graph_structures.d(124): Error: class graph_structures.node(D, > E) is used as a type > > I have no idea what this means:( It usually means you tried to use an uninstantiated template as a type. [...] > auto fee = new node(string, u_edge)("Suck Moose", 1); [...] You're missing the compile-time argument "!" operator; this line should be written as: auto fee = new node!(string, u_edge)("Suck Moose", 1); Hope this helps. T -- Too many people have open minds but closed eyes. | |||
June 03, 2014 Re: Fighting compiler - experienced programmer but D novice | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Charles Parker | On Tuesday, 3 June 2014 at 03:17:10 UTC, Charles Parker wrote:
> ...
> Thanx for any help - Charlie
Well one thing is that you don't need the type parameters on the
this function. You're basically creating a templated this inside
the templated class which is not what you want.
try this:
class node(D, E) {
int nid;
D data;
E[] in_edges; // All edges for undirected graphs go here.
E[] out_edges; // Only used by directed graphs
this(D data, int n) {
this.data = data;
nid = n;
in_edges = new E[];
out_edges = new E[];
}
Another thing is that you're missing the ! to instantiate
v
auto fee = new node!(string, u_edge)("Suck Moose", 1);
^
| |||
June 03, 2014 Re: Fighting compiler - experienced programmer but D novice | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Chris Cain | On Tuesday, 3 June 2014 at 03:35:46 UTC, Chris Cain wrote:
> On Tuesday, 3 June 2014 at 03:17:10 UTC, Charles Parker wrote:
>> ...
>> Thanx for any help - Charlie
>
> Well one thing is that you don't need the type parameters on the
> this function. You're basically creating a templated this inside
> the templated class which is not what you want.
>
> try this:
>
> class node(D, E) {
> int nid;
> D data;
> E[] in_edges; // All edges for undirected graphs go here.
> E[] out_edges; // Only used by directed graphs
>
> this(D data, int n) {
> this.data = data;
> nid = n;
> in_edges = new E[];
> out_edges = new E[];
> }
>
> Another thing is that you're missing the ! to instantiate
>
> v
> auto fee = new node!(string, u_edge)("Suck Moose", 1);
> ^
Chris, that was it I needed to do both things. It then complained
about trying to allocate the in_edges and out_edges arrays in the
constructor which is how I thought dynamic arrays are allocated
on the heap. I removed the 2 new statements, and both compile and
execution of my initial test worked.
Thanx - Charlie
| |||
June 03, 2014 Re: Fighting compiler - experienced programmer but D novice | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Charles Parker | On 03/06/14 05:57, Charles Parker wrote: > Chris, that was it I needed to do both things. It then complained > about trying to allocate the in_edges and out_edges arrays in the > constructor which is how I thought dynamic arrays are allocated > on the heap. I removed the 2 new statements, and both compile and > execution of my initial test worked. You don't really need to allocate the arrays. You can just declare them and start using them: int[] foo; foo ~= 4; // append 4 to the array Arrays in D are a implemented as a struct with a pointer to the data and a field with the length of the array. -- /Jacob Carlborg | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply