Hi I want to display a variable menber array queue, and the definitions of the class are The Array.h

template <class DataType>
class Array
{
public:
Array( int size );

private:
T *ele;
};

The Array.cpp

Array<T>::Array( int size )
{
if ( size < 1 ) {
capacity = 1;

}
else {
capacity = size;

}

ele = new T [capacity];
}

Q.h

#include "Array.h"
template <class T>
class Queue
{
public:
Queue( );
void enqueue( T ele );
private:
Array<T> ele;
int front;
int back;
};

Q.cpp

template <class T>
Queue<T>::Queue( )
: ele( 2 ), front( -1 ), back( -1 )
{
}
template <class T>
void Queue<T>::enqueue( T ele )

in the main I have

// I declare a function template fro display the queue
template <T>
void Disply(const DataType elem[] )
{

for( int i = 0; i<6; i++)

cout<< "The elements of the Queue are:\n", i , elem[i]<<endl;

}

int main()
{

Queue<int> elementsArQueue;

int array[]={1,2,3,4,5,6};

for (int a=0;a<6;a++)

queue.enqueue(array);

Display(elementsArQueue.);// give to me some errors like

//P.cpp:

In function int main():
P.cpp:48: error: no matching function for call to Display(Queue<int>&)

Dani AI

Generated

Quick diagnosis (what's actually failing)
The compiler error comes from mismatched templates and types: your Display template doesn't match a Queue<int> object, the template header is malformed, and you try to pass a whole C array to enqueue (which expects a single element). was right to flag template-definition placement and the need for template<...> before every out‑of‑class template definition.

Concrete, minimal fixes and an interface you can use

  • Keep both the template declarations and their implementations in headers (or include the .cpp from the header). The compiler must see the template definition at instantiation time unless you provide explicit instantiations.
  • Use clear member names (avoid reusing the same name for a member and for a parameter).
  • Make Queue expose a stable read interface (size() and an accessor) so a printing function can take a const Queue<T>&.

Example of a tiny, header-only interface you can adapt:

// small, different example — not a copy of your posted code
template<typename T>
class SimpleArray {
public:
  explicit SimpleArray(std::size_t n = 1);
  ~SimpleArray();
  T& operator[](std::size_t i);
  std::size_t size() const;
private:
  std::size_t n_;
  T* data_;
};

template<typename T>
class RingQueue {
public:
  explicit RingQueue(std::size_t cap = 8);
  void enqueue(const T& value);
  std::size_t size() const;
  const T& at(std::size_t index) const; // index 0..size()-1
private:
  SimpleArray<T> buf_;
  std::size_t head_, count_;
};

How to print and how to call enqueue

  • Define a printer that accepts const RingQueue<T>& (not a raw T[]), for example:
template<typename T>
void display(const RingQueue<T>& q) {
  for (std::size_t i = 0; i < q.size(); ++i)
    std::cout << q.at(i) << '\n';
}
  • To fill the queue from a C array, push elements one at a time:
    for (size_t i = 0; i < N; ++i) q.enqueue(arr[i]);

Quick checklist for debugging

  • Replace template <T> with template<typename T> (or template<class T>).
  • Move template implementations into headers or explicitly instantiate the types you need.
  • Make sure identifiers match (DataType vs T) and fix the cout commas (use <<, not commas).
  • Consider using std::vector/std::queue to avoid manual memory bugs if you don't need a learning exercise.

This addresses the compile error and gives a clean path forward without relying on separate .cpp instantiations.

Recommended Answers

All 2 Replies

Check out:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13

It's touchy about template parameters being defined in separate compilation units. It probably has to do with your DataType parameter being used in a function with your T parameter in main. I did not try your code so I'm not positive.

P.S. What's up with that font? hehe

There are many other things, but you need to have template <class T> before every function definition that uses T (e.g. in array.cpp). Also you pull capacity out of thin air. Make sure your function names are spelled correctly as, within the code you posted you have "Disply" as your function name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.