CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Generic Classes

  1. #1
    Join Date
    Jan 2010
    Posts
    5

    Angry 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>

  2. #2
    Join Date
    Jan 2010
    Posts
    5

    Exclamation 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

  3. #3
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: Generic Classes

    Are the classes NodeType and EdgeType defined in a custom namespace you forgot to import using 'using'?

  4. #4
    Join Date
    Jan 2010
    Posts
    5

    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()

  5. #5
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    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..

  6. #6
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: Generic Classes

    Quote Originally Posted by kegtappa View Post
    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>

  7. #7
    Join Date
    Jan 2010
    Posts
    5

    Resolved Re: Generic Classes

    Quote Originally Posted by rohshall View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured