-2

I'm trying to implement the following code, but I keep getting an error because of typedef, can someone please help?

 template<class T>
 struct box{
 T data;
 box *link;
 };
 typedef box* boxPtr;

the error is:

Use of class template 'box' requires template arguments
cigien
  • 57,834
  • 11
  • 73
  • 112
harry
  • 47
  • 8

4 Answers4

2

Writing

template<class T>
struct box{
    T data;
    box *link;
 
    typedef box<T>* boxPtr; 
};

with

int main()
{
    box<int>::boxPtr use_of_a_bad_idea;
}

is one way, but having pointer types masquerading as object types is a recipe for memory leaks: don't do it as a rule of thumb. That's the best way of addressing this problem.

cigien
  • 57,834
  • 11
  • 73
  • 112
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    *"having pointer types masquerading as pointer types"* - Ah... um... – StoryTeller - Unslander Monica Nov 09 '20 at 18:02
  • Not so long ago, I misread a question and wrote it off as a trivial matter of pass by value where pass by reference was required because the asker used a typedef to conceal that the type *was* a pointer. I could have just as easily given bad advice if I hadn't read through the code several times. the `Ptr` in `boxPtr` makes the pointer orders of magnitude more obvious than the other lost question, but still not as obvious as `box *` for anyone who's brain is conditioned to look for the `*`. – user4581301 Nov 09 '20 at 18:15
  • @StoryTeller-UnslanderMonica: Had a delivery today which required an excavator to make lots of turns over my once-pristine lawn. It now looks like a MOD training ground. Very stressful indeed. Hence the odd mistake or two creeping in. – Bathsheba Nov 09 '20 at 21:35
2

box is a class template. This means that it needs an argument whenever you instantiate it:

box<int>   integerBox;
box<float> floatBox;
// etc

This is required anywhere you try to use a template class such as box. So a pointer to just a box doesn't make sense. It needs a type with it. For example:

template <class type>
using boxptr = box<type>*;

This does effectively the same thing as your typedef, but it allows you to specify he underlying template type.

Of course, if you do it this way, you will always need to specify a type when you use the pointer version:

boxptr<int>   integerBoxPtr;
boxptr<float> floatBoxPtr;
// etc
  • @harry You're welcome. Please note that on this site, the best way to thank someone is to up-vote their answer and/or accept it. We try to avoid comments in general on this site except when absolutely necessary. Thank you comments are usually not considered absolutely necessary--again we usually prefer you to up-vote and accept the best answer instead. –  Nov 09 '20 at 22:58
1

Use using:

template<class T>
struct box
{
    T data;
    box *link;
};

template <typename T>
using boxPtr = box<T>*;

Now boxPtr<int> is the same as box<int>*.

0

This will compile:

template<class T>
struct Box {
    T data;
    Box<T> *link;
};

typedef Box<int> * BoxPTR;
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36