|
-
March 9th, 2010, 08:17 PM
#1
Generic Classes
I'm having some trouble with a generic class I'm trying to create. I'd like to use this class to perform DFS searches on different variations of a base graph class, but the compiler doesn't agree with me. The errors I'm getting are: unknown type "NodeType", and unknown type "EdgeType". (Which I think is funny because intellisense pickups up the definition just fine and shows me a tool tip with the class definition of the SparseGraph class including the where clause. So I know it KNOWS the definition I'm trying to use... but I digress)
Code:
class SearchGraphDFS<GraphType> where GraphType : SparseGraph<NodeType,EdgeType>
class SparseGraph<NodeType, EdgeType> where NodeType : GraphNode, new() where EdgeType : GraphEdge, new()
class GraphEdge
class GraphNode
So to be clear the "SearchGraphDFS" class is the problem. The compiler (VS2008) errors on NodeType and EdgeType. The other 3 class definitions are included for clarity.
The question is if my way isn't the correct way to implement what I'm trying to achieve, then what is? Or is there a way I can define NodeType and EdgeType without having to do something like this?
Code:
class SearchGraphDFS<GraphType,NodeType,EdgeType> where GraphType : SparseGraph<NodeType,EdgeType>
-
March 9th, 2010, 08:22 PM
#2
Re: Generic Classes
Uh oh yeah, I forgot to give my versions.
Visual Studio 2008 Version 9.0.30729.1 SP
.NET Framework Version 3.5 SP1
-
March 9th, 2010, 09:48 PM
#3
Re: Generic Classes
Are the classes NodeType and EdgeType defined in a custom namespace you forgot to import using 'using'?
-
March 9th, 2010, 10:44 PM
#4
Re: Generic Classes
No, NodeType and EdgeType are generic type identifiers defined in my "SparseGraph" class.
Code:
class SparseGraph<NodeType, EdgeType> where NodeType : GraphNode, new() where EdgeType : GraphEdge, new()
-
March 10th, 2010, 12:05 AM
#5
Re: Generic Classes
try to reverse the order in which you have declared your classes and see .. like this :
Code:
class GraphEdge
class GraphNode
class SparseGraph<NodeType, EdgeType> where NodeType : GraphNode, new() where
EdgeType : GraphEdge, new()
class SearchGraphDFS<GraphType> where GraphType : SparseGraph<NodeType,EdgeType>
may be the compiler is trying to find the declarations/definitions of those subclasses first..
-
March 10th, 2010, 12:35 AM
#6
Re: Generic Classes
 Originally Posted by kegtappa
Code:
class SearchGraphDFS<GraphType,NodeType,EdgeType> where GraphType : SparseGraph<NodeType,EdgeType>
Under where constraint, you can specify either a class or an interface. However, in this case, your constraint is a generic class and not a concrete class.
So, you need to give the concrete types for SparseGraph class instantiation like
SparseGraph<GraphNode, GraphEdge>
-
March 10th, 2010, 12:57 AM
#7
Re: Generic Classes
 Originally Posted by rohshall
Under where constraint, you can specify either a class or an interface. However, in this case, your constraint is a generic class and not a concrete class.
So, you need to give the concrete types for SparseGraph class instantiation like
SparseGraph<GraphNode, GraphEdge>
Seems to be working so far, thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|