"template container" declaration error

V

vl_

Hello,

I am trying to declare "containerr" class for the template, so that it
would contain explicit size for the templated object:

MyClass.hpp
#include <queue>
template <typename T>
// Container class
class Element
{

public:
Element( T t, int size)
{
m_t = t;
m_size = size;
}
virtual ~Element() {};
T m_t;
int m_size;
} ;
// Queue wrapper
template <typename T>
class MyQueue
....
//STL queue declaration:
std::queue <Element> m_queue;

.....
I have the following error: "ISO C++ forbids declaration of `m_queue'
with no type,
expected a type, got `Element'"

Why doesn't compiler see declaration of type 'Element' ?
Thank you,
 
S

SG

#include <queue>
template <typename T>
// Container class
class Element
[...]

//STL queue declaration:
std::queue <Element> m_queue;

....
I have the following error: "ISO C++ forbids declaration of `m_queue'
with no type,
  expected a type, got `Element'"

The class template std::queue expects a type parameter. "Element"
however is a template. I see what you are trying to do. But this is
not going to work. You need an additional level of indirection. Here
are a couple of pointers that could help you:
- Store smart pointers in the queue (shared_ptr<abstract_base_class>)
- Do a search on "C++ envelope letter idiom"
- Do a search on "C++ type erasure"
- Adobe's PolyValue library

HTH,
SG
 
V

vl_

#include <queue>
template <typename T>
// Container class
class Element
[...]

//STL queue declaration:
std::queue <Element> m_queue;
....
I have the following error: "ISO C++ forbids declaration of `m_queue'
with no type,
expected a type, got `Element'"

The class template std::queue expects a type parameter. "Element"
however is a template. I see what you are trying to do. But this is
not going to work. You need an additional level of indirection. Here
are a couple of pointers that could help you:
- Store smart pointers in the queue (shared_ptr<abstract_base_class>)
- Do a search on "C++ envelope letter idiom"
- Do a search on "C++ type erasure"
- Adobe's PolyValue library

HTH,
SG

Thanks for the pointers. Would it be possible without "boost"?
Thanks again...
 
I

Ingo Nolden

vl_ said:
#include <queue>
template <typename T>
// Container class
class Element [...]

//STL queue declaration:
std::queue <Element> m_queue;
....
I have the following error: "ISO C++ forbids declaration of `m_queue'
with no type,
expected a type, got `Element'"
The class template std::queue expects a type parameter. "Element"
however is a template. I see what you are trying to do. But this is
not going to work. You need an additional level of indirection. Here
are a couple of pointers that could help you:
- Store smart pointers in the queue (shared_ptr<abstract_base_class>)
- Do a search on "C++ envelope letter idiom"
- Do a search on "C++ type erasure"
- Adobe's PolyValue library

HTH,
SG

Thanks for the pointers. Would it be possible without "boost"?
Thanks again...
You are sure you didn't want to write

std::queue <Element<T> > m_queue;

?
 
J

James Kanze

I am trying to declare "containerr" class for the template, so
that it would contain explicit size for the templated object:
MyClass.hpp
#include <queue>
template <typename T>
// Container class
class Element
{
public:
Element( T t, int size)
{
m_t = t;
m_size = size;
}
virtual ~Element() {};
T m_t;
int m_size;
} ;
// Queue wrapper
template <typename T>
class MyQueue
...
//STL queue declaration:
std::queue <Element> m_queue;
....
I have the following error: "ISO C++ forbids declaration of
`m_queue' with no type,
expected a type, got `Element'"
Why doesn't compiler see declaration of type 'Element' ?

The compiler can see the definition of Element. That's how it
knows that it's not a type. Otherwise, it would probably give
an error message along the lines of "unknown symbol".

A template is not a type. A class template is a specification
which can be instantiated to create a type, but only the
instantiations are types. You need something along the lines
of:
std::queue< Element< T > > m_queue ;
 
V

vl_

The compiler can see the definition of Element. That's how it
knows that it's not a type. Otherwise, it would probably give
an error message along the lines of "unknown symbol".

A template is not a type. A class template is a specification
which can be instantiated to create a type, but only the
instantiations are types. You need something along the lines
of:
std::queue< Element< T > > m_queue ;

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

You can't declare type based on template, so your syntax as wrong as
my original approach... Thanks, though.
 
J

James Kanze

You can't declare type based on template, so your syntax as
wrong as my original approach... Thanks, though.

Of course you can. My syntax is correct according to the
standard, and works on all of the compilers I have access to.
I use it fairly regularly.
 
I

Ivan

Hello,

I am trying to declare "containerr" class for the template, so that it
would contain explicit size for the templated object:

MyClass.hpp
#include <queue>
template <typename T>
// Container class
class Element
{

public:
Element( T t, int size)
{
m_t = t;
m_size = size;
}
virtual ~Element() {};
T m_t;
int m_size;} ;

// Queue wrapper
template <typename T>
class MyQueue
...
//STL queue declaration:
std::queue <Element> m_queue;

....
I have the following error: "ISO C++ forbids declaration of `m_queue'
with no type,
expected a type, got `Element'"

Why doesn't compiler see declaration of type 'Element' ?
Thank you,


As I know, Element is a class template rather than a type or class. In
fact Element<int> is a type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top