0

I'm having a hard time understanding template class implementation in C++. I understand what a template class is and how to use it, but I cannot seem to implement them properly. This is for school so I cannot use standard library list/etc. I have made a template List class that acts as a linked list using a template node class. I have a third class bigInt which will be used to do infinite precision addition, multiplication, etc. For the bigInt class I get an error when trying to have a variable "values" that is of type List. Why is this? Error: "Error C2079 'bigInt::values' uses undefined class 'List'"

bigInt.h looks like:

template <typename T>
class List;
class bigInt {
public:
    List<int> values;
    bigInt();
    bigInt add(bigInt);
    bigInt mul(bigInt);
    bigInt pow(int);
};

I added the first two lines because I read somewhere that I needed to use "forward declaration" (since you apparently cannot use an #include "List.h") which I also don't really understand.

Any help would be really appreciated.

1 Answers1

0

You need to completely define the class List<> before you can use it as a member variable. This is usually done by defining the template class in a separate .h file and #includeing it where needed (not sure why you think you can't do this). Alternatively, you can use a pointer to a List without defining it first:

template <typename T>
class List;

class bigInt {
public:
  List<int>* values;
  /*...*/
}
Carlton
  • 4,217
  • 2
  • 24
  • 40