0

I am a beginner both in programming and in C++. I have used templates before but in a very limited way and I have no idea what I am doing wrong:

template <typename TElement>
struct list{    // if I try list<TElement> => list is not a template
    TElement data;
    struct list *next;
} node_type;    // also tried node_type<TElement>, other incomprehensible errors
node_type *ptr[max], *root[max], *temp[max];

I find the error a bit incomprehensible: "node_type does not name a type"
What am I doing wrong ?

What I intend to do:
I want to have a list of type (so I can use this on several completely different Abstract Data Types - ADTs), so I'd like to end up with something like this:

Activities list *ptr[max], *root[max], *temp[max]

if that makes any sense (Where Activities is a class but can be any other class).

Joe
  • 41,484
  • 20
  • 104
  • 125
Kalec
  • 2,681
  • 9
  • 30
  • 49
  • 4
    I think you would probably be best off picking up a good book on C++ and working your way up. C++ doesn't work like that; it's unclear what you're trying to achieve; and it's unlikely that this is a productive way to learn. – Kerrek SB May 29 '12 at 15:51
  • this looks like old C-style a la `typedef struct { ... } name;` but without the `typedef`. – Walter May 29 '12 at 15:54
  • Btw, I find the error perfectly comprehensible. What else do you want for an error message here? – Walter May 29 '12 at 15:56
  • I don't actually know what I would have wanted the error to say, but I still have no idea what it does say. I also agree this is not a good way to learn c++ but as counter - intuitive as it sounds ... at my school learning c++ is not important, because no one is teaching c++. We are expected to know stuff and pass the exam ... but that's a problem for another time. – Kalec May 29 '12 at 16:11

2 Answers2

5

Try this:

template <typename TElement>
struct node{   
    TElement data;
    node* next;
};    

node<int>* ptr[max], *root[max], *temp[max];

One additional suggestion: avoid naming your custom types after types from standard C++ library (like list, vector, queue...which are all in namespace std). It is confusing and can result with name clashes (unless you place it in your own namespace which you'll need explicitly to use wherever you put using namespace std;).

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • I need to use Several class objects (abstract data types) with this, which will soon end up being a self implemented dictionary (homework) so using would be counter productive because the only reason I used the template is to use anything I want there. – Kalec May 29 '12 at 15:53
  • You can use any type as a template parameter; I put `int` just as an example – Bojan Komazec May 29 '12 at 15:56
  • But I need it to be TElement and if I try `node_type`, TElement is "not declared in this scope". – Kalec May 29 '12 at 15:57
  • You want to create your own list type which can hold data of *any* type. That is the reason why you made this struct as a template: compiler will replace `TElement` with the actual (*any*) type provided (`int` in my example) – Bojan Komazec May 29 '12 at 16:01
  • uhum, so the compiler will replace with my type. I understand, but I don't get why do I have to use int instead of my specified type, it is very confusing and counter-intuitive (for me any way). – Kalec May 29 '12 at 16:03
  • No, try reading what he wrote. You replace `int` with your specified type, so you write `node_type` – Jonathan Wakely May 29 '12 at 16:12
  • Just specify YOUR type instead of int as in the example. The compiler replaces TELement with whatever you put there. – πάντα ῥεῖ May 29 '12 at 16:12
  • No. Compiler will replace `TElement` with the actual type. You named struct in a non-intuitive way so that's probably what confuses you. I will edit my post and rename it to (just) `node`. If you have `node* pNode;` compiler will generate a new type replacing TElement with `int` - you'll have a list of integers. You will use `node` if you want to have a list with nodes that contain `MyCustomType` objects. – Bojan Komazec May 29 '12 at 16:14
  • What you might be missing is that at some point you need to tell the compiler what type to use for TElement. That's what does in this example. If you're hoping that you'll be able to store many different data types in the same list, this is not the way to do it. This defines ONE list with a pre-defined datatype. – Peter May 29 '12 at 16:16
  • In your case you would write `node_type` if you want a list of type `Activites`. But you should probably use `std::list` until you understand the basics – Jonathan Wakely May 29 '12 at 16:19
2

Don't try to learn C++ by trial and error and guessing at the syntax, read a good book.

template <typename TElement>
  struct list{

This declares a struct template called list which has a single template parameter, TElement, which serves as an alias (only within the body of the template) for the type you instantiate the template with.

If you instantiate list<int> then TElement will refer to int. If you instantiate list<char> then TElement will refer to char. etc.

So the type you instantiate the template with will be substituted for TElement in your template definition.

The error you got when trying:

// if I try list<TElement> => list is not a template
template <typename TElement>
  struct list<TElement> {

is because this is not valid syntax, the error is telling you list isn't a template because at the point where you write list<TElement> you haven't finished declaring list yet, so the compiler has no idea what it is and you can't have a list of something if the list template isn't defined.

template <typename TElement>
  struct list{
    TElement data;
    struct list *next;
  }node_type;

This attempts to declares an object of type list, similar to this syntax:

struct A {
  int i;
} a;        // the variable 'a' is an object of type 'A'

But in your case list is not a type, it's a template. list<int> is a type, but list on its own is not a valid type, it's just a template that a type can be made from when you "fill in the blanks" i.e. provide a type to substitute for the parameter TElement

It looks like you weren't even trying to declare a variable anyway, just blindly guessing at syntax.

// also tried node_type<TElement>, other incomprehensible errors

That won't help either, node_type<TElement> is nonsense, if you want to declare a variable it needs a type, e.g. list<int>. The parameter TElement needs to be replaced with a type, it isn't a type itself. Stop trying to string together random bits of syntax hoping it will work. You could start by reading http://www.parashift.com/c++-faq/templates.html

On the last line:

node_type *ptr[max], *root[max], *temp[max];

node_type isn't a type so that won't work. Also, you should avoid getting into the bad habit of declaring multiple variables on a single line. It's much clearer to write:

int* p1;
int* p2;

Instead of

int *p1, *p2;

Also, are you sure youu want arrays of pointers? Since you clearly don't know what you're doing it would be more sensible to use standard container types that do the work for you.

Community
  • 1
  • 1
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Thank you very much. I suppose you are right about not trying trough trial and error, but time does not permit me to learn c++ trough a book. I like programming but I am not dedicated enough to learn a new language every 4 months to pass some random exam, yet that is not your problem :P – Kalec May 29 '12 at 16:30