Thread: Template compiler pains

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    16

    Template compiler pains

    Okay, so I've got a simple templated d-heap I'm trying to get compiled here. This is ironic because I'm trying to use this as part of my almost complete graph data structure and I can't even get something that should be simple to work! This is my first attempt at a template so maybe I'm missing something obvious. Anyways, here's some relevant bits of code:

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    template <class T,int dval>
    class Heap
    {
        private:
            vector<T> v;
    
        public:
            // constructor
            Heap(void);
            // heap methods
            bool empty(void) const;
            void insert (const T);
            T remove (void);
    };
    
    template <class T>
    Heap<T>::Heap (void)
    {
        //constructor stuff here
    }
    .
    .
    .
    and here's some compilier errors:

    Code:
    In file included from graph.h:7,
                     from main.cpp:2:
    heap.h:10: `Heap' is not a template type
    heap.h:12: data member `v' cannot be a member template
    heap.h:24: non-template type `Heap' used as a template
    heap.h:25: ISO C++ forbids declaration of `Heap' with no type
    heap.h:30: syntax error before `::' token
    .
    .
    .
    Thanks in advance for any direction!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    template <class T,int dval>
    class Heap
    {
        // [...]
    };
    
    template <class T>
    Heap<T>::Heap (void) {
    Your templates don't match.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Ooops, that was an addition. Anyways, same errors after I fixed that.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    template <class T, int dval>
    Heap<T,dval>::Heap()
    {
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Quote Originally Posted by JaWiB
    Code:
    template <class T, int dval>
    Heap<T,dval>::Heap()
    {
    }
    Yeah, that's what I have now but I still get the same errors.

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    template <class T,int dval>
    class Heap
    {
        private:
            vector<T> v;
    
        public:
            // constructor
            Heap(void);
            // heap methods
            bool empty(void) const;
            void insert (const T);
            T remove (void);
    };
    
    template <class T,int dval>
    Heap<T,dval>::Heap (void)
    {

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    This compiles fine for me:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template <class T,int dval>
    class Heap
    {
        private:
            vector<T> v;
    
        public:
            // constructor
            Heap();
            // heap methods
            bool empty(void) const;
            void insert (const T);
            T remove (void);
    };
    
    template <class T, int dval>
    Heap<T,dval>::Heap()
    {
        //constructor stuff here
    }
    
    int main()    
    {
      
    }
    There's probably code you're not showing us that is causing the problem.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Quote Originally Posted by JaWiB
    This compiles fine for me:

    There's probably code you're not showing us that is causing the problem.
    Yeah, I just tested it and it's not the heap code at all...that compiles fine. Is it that way I'm make-ing maybe? What code would be helpful to post?

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Here's my make file:

    Code:
    #Makefile Template
    
    TARGETS = main
    
    CXX = g++
    CXXFLAGS = -Wall -c
    LDFLAGS = -Wall
    
    all: $(TARGETS)
    
    main: main.o edge.o vertex.o graph.o
            $(CXX) $(LDFLAGS) $^ -o $@
    
    main.o: main.cpp graph.h
            $(CXX) $(CXXFLAGS) $<
    
    graph.o: graph.cpp graph.h edge.h vertex.h heap.h
            $(CXX) $(CXXFLAGS) $<
    
    edge.o: edge.cpp edge.h vertex.h
            $(CXX) $(CXXFLAGS) $<
    
    vertex.o: vertex.cpp vertex.h
            $(CXX) $(CXXFLAGS) $<
    
    # usage
    
    # test: main.o class.o
    #       $(CXX) $(LDFLAGS) $^ -o $@
    
    # main.o: main.cpp class.h
    #       $(CXX) $(CXXFLAGS) $<
    
    clean:
            rm -f *.o $(TARGETS)

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Here's an example of how I'm instansiating the heap:

    Code:
    // heap of nodes not in MST
    Heap<Edge, 2> h;
    // temp storage
    Heap<Edge, 2> h2;
    Edge is one of my own data types. The compile errors are completely limited to heap.h. But I copied heap.h into a file like this:

    Code:
    <heap.h code here>
    
    int main ()
    {
    }
    and it compiled fine.

    This is so confounding. This is the last thing I need to get my graph working. Ideas anyone?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What are the errors if you put Heap<int, 2> h; inside your empty main? You won't get compile errors on a templated class unless you use it, which is probably why the empty main works.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    heap.h:10: `Heap' is not a template type
    Try make clean and then recompiling it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    16
    Quote Originally Posted by Daved
    What are the errors if you put Heap<int, 2> h; inside your empty main? You won't get compile errors on a templated class unless you use it, which is probably why the empty main works.
    That works fine. However, I figured out if I tried to do Heap<Edge, 2> h; that it fails with the exact same errors. So why is it failing? Do I have to do something special to my Edge class to make it useable in the template?

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The template parameter must support all operations performed on it. If your Edge class is copyable (which it is, unless you've hidden the copy constructor), you should be all set.
    On a side note, it's very bad practice to have using directives or statements in header files, but I doubt that's where your problem lies.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I figured out if I tried to do Heap<Edge, 2> h; that it fails with the exact same errors.
    Post your Edge class if you're still not sure what the problem is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template to let compiler generate zero
    By KIBO in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2009, 03:51 AM
  2. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  3. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 02:10 AM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. template and compiler ?!!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-28-2002, 03:47 AM