templated class in multiple files

This is a discussion on templated class in multiple files within the C++ Programming forums, part of the General Programming Boards category; I am writing the functions for a class that is templated using 3 files: qpqueue.h, qpqueue.cpp, and main.cpp here's qpqueue.h: ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    templated class in multiple files

    I am writing the functions for a class that is templated using 3 files: qpqueue.h, qpqueue.cpp, and main.cpp
    here's qpqueue.h:
    Code:
    #include <queue>
    const int MAXPRIORITY = 10;
    
    template <typename T>
    class qpqueue
    {
      public:
        qpqueue();
    
        void push (const T & item, int p);
        // insert item with priority p, 
        // 0 <= p <= MAXPRIORITY
    
        void pop();
         // find the non-empty queue with largest index and remove its
         // front element
         // Precondition: the queue is not empty, the function throws
         //      underflowError exception if the queue is empty
    
         T& top(); 
         // find the non-empty queue with largest index and return 
         // its front element.
         // Precondition: the queue is not empty, the function 
         // throws the underflowErr exception if the queue is empty
    
         const T& top() const;
         // constant version of top();
    
         bool empty() const;
         // is the queue empty?
    
         int size() const;
         // return the number of elements in the queue
    
      private:
         queue<T> priority[MAXPRIORITY +1];
          // priority[i] contains all elements with priority i 
          //   in their order of insertion
         int pqsize;
          // number of elements in the priority queue
    };
    When I try to compile my project, I get an error saying, "ISO C++ forbids declaration of 'queue' with no type."
    I'm not sure if there's a problem with the implementation of my qpqueue.cpp file, or if I just need to have code in main() that actually implements the qpqueue class with a specified type. Thanks.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    What's the line with the error?

    Generally, you have to implement templated classes all within the header file, or #include the cpp file at the bottom of the header file. You probably won't see that error until you get the current one fixed, though. My guess is you put queue:: without the <T> somewhere in the cpp file.

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    the line with the error is in private when i declare queue<T> priority[MAXPRIORITY + 1];
    I'm not sure how to do the .cpp file... here's what i have:
    Code:
    #include "qpqueue.h"
    
    qpqueue<T>::qpqueue()
    {
        pqsize = 0;
    }
    
    void qpqueue<T>::push (const T & item, int p)
    {
        if (0 <= p && p <= MAXPRIORITY)
        {
            priority[p].push(item);
            pqsize++;
        }
    }
    is that right?
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Change it to std::queue to fix the error.

    For your cpp file, you need template <typename T> before each function definition.

    And finally, you'll need to #include "qpqueue.cpp" at the bottom of qpqueue.h inside the include guard (if you have an include guard-- if you don't, add one). This is the only time it makes sense to #include a cpp file.

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    in my .cpp file, my constructor is this:
    Code:
    template <typename T>
    qpqueue<T>::qpqueue()
    {
        pqsize = 0;
    }
    obviously that's wrong... what did I do wrong?

    EDIT:
    i figured out my problem: i did not have the include guard in the .cpp file, it was only in the .h file, and since i included the .cpp file in the .h file, i figured i didn't need to include the .h file in the .cpp file. circular includes and stuff. but now it's all compiling. thanks for the help Daved
    Last edited by linucksrox; 03-31-2006 at 05:12 PM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I don't know if I understand your edit, but the general way to do it is to put include guards around everything in your header file only. Then add the #include "qpqueue.cpp" inside the include guard at the bottom of the header. In the cpp file, you should #include "qpqueue.h" and you should not have include guards. It will work because of the include guards inside the header file.

    File.h:
    Code:
    #ifndef FILE_H
    #define FILE_H
    
    // template class declaration
    
    #include "File.cpp"
    
    #endif // FILE_H
    File.cpp:
    Code:
    #include "File.h"
    
    // class function definitions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-22-2009, 11:30 PM
  2. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  3. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21