Using typedef struct in a template class

Hi,

I want to use what I call a "Chainage" in a template class. Here is my hpp code :

typedef template < K, V > struct _maillon { // parametrized by types K and V
K clef;
V valeur;
struct _maillon *succ;
} Maillon;

typedef struct {
Maillon * tete;
Maillon * queue;
int nb_elts;

} Chainage;


template < typename K, typename V >


class AList{
inc
private :

Chainage ch; // Here I use the type i've defined up there

...
}#include "alist.tpp"

And the template will define all the methods. But I have the following compilation problem :

"alist.hpp:16:9: erreur: expected unqualified-id before ‘template’
alist.hpp:20:4: erreur: ‘Maillon’ does not name a type
alist.hpp:23:2: erreur: ‘Maillon’ does not name a type
alist.hpp:24:2: erreur: ‘Maillon’ does not name a type"




I don't know how to solve it...

Thanks for your help :)

You're misusing the keyword 'typedef'. If you saw somewhere someone writing typedef struct a {...} b;, that was C code. in C++, we write struct b {...};.

try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>

template< typename K, typename V >
struct Maillon {
    K clef;
    V valeur;
    Maillon *succ;
};

template<typename K, typename V>
struct Chainage {
    Maillon<K,V> * tete;
    Maillon<K,V> * queue;
    int nb_elts;
};

template<typename K, typename V>
class AList {
    Chainage<K, V> ch;
};

int main()
{
    AList<int, std::string> al;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class K, class V>
struct _maillon{
K clef;
V valeur;
struct _maillon *succ;
};

typedef _maillon<int, int> Maillon;

typedef struct {
Maillon *tete;
Maillon *queue;
int nb_elts;
} Chainage;


the code i posted above compiles just fine and should suite your needs.
You cant typedef a template struct when defining the template struct from what ive been told. you define the struct then typedef once the struct has been defined.

also i used ints as the types but that could be changes as needed. thats one of the things you were missing.
Last edited on
Topic archived. No new replies allowed.