Creating a new Template Item
-
Hi, I made a Stack implementation, and now I'm looking to make the implementation be more generic. I know that the way to do this is probably to use template. I have a cons list of items which is
template class ConsList : public LList { .... ConsList(T first, LList* rest); //constructor }
as of now. When I try to make an instance of the ConsList with the new keyword, I get a lot of errorsg++ -o Main LList.o Stack.o Exception.o main.o Stack.o: In function `Stack::push(int)': Stack.cpp:(.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0xe5): undefined reference to `EmptyList::EmptyList()' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0x123): undefined reference to `EmptyList::EmptyList()' collect2: ld returned 1 exit status
I've googled templates, and it looks like nobody makes a new item that is a template. If there a reason why people don't use this, or how would I go about creating an instance of ConsList(.., ..). Thanks, -Ken -
Hi, I made a Stack implementation, and now I'm looking to make the implementation be more generic. I know that the way to do this is probably to use template. I have a cons list of items which is
template class ConsList : public LList { .... ConsList(T first, LList* rest); //constructor }
as of now. When I try to make an instance of the ConsList with the new keyword, I get a lot of errorsg++ -o Main LList.o Stack.o Exception.o main.o Stack.o: In function `Stack::push(int)': Stack.cpp:(.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0xe5): undefined reference to `EmptyList::EmptyList()' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0x123): undefined reference to `EmptyList::EmptyList()' collect2: ld returned 1 exit status
I've googled templates, and it looks like nobody makes a new item that is a template. If there a reason why people don't use this, or how would I go about creating an instance of ConsList(.., ..). Thanks, -KenMaybe select the "Ignore HTML tags in this message" check box and re-post your code. There are a lot of sad faces in it.
- S 50 cups of coffee and you know it's on!
-
Hi, I made a Stack implementation, and now I'm looking to make the implementation be more generic. I know that the way to do this is probably to use template. I have a cons list of items which is
template class ConsList : public LList { .... ConsList(T first, LList* rest); //constructor }
as of now. When I try to make an instance of the ConsList with the new keyword, I get a lot of errorsg++ -o Main LList.o Stack.o Exception.o main.o Stack.o: In function `Stack::push(int)': Stack.cpp:(.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0xe5): undefined reference to `EmptyList::EmptyList()' Stack.o: In function `Stack::Stack()': Stack.cpp:(.text+0x123): undefined reference to `EmptyList::EmptyList()' collect2: ld returned 1 exit status
I've googled templates, and it looks like nobody makes a new item that is a template. If there a reason why people don't use this, or how would I go about creating an instance of ConsList(.., ..). Thanks, -KenKen Mazaika wrote:
Stack.cpp.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)'
It seems that you have n't defined the functions of the template classes. At the point of template instantiation the definition of the function should be visible where it is triggered by refering in the code (of stack). May be you declared the template class in a header file which doesnot have definitions (possibly you defined in LList.cpp) and included the header in stack.cpp. But template class requires the definitions also , so add the definitions in LList.cpp to the header itself.
-
Ken Mazaika wrote:
Stack.cpp.text+0xaa): undefined reference to `ConsList::ConsList(int, LList*)'
It seems that you have n't defined the functions of the template classes. At the point of template instantiation the definition of the function should be visible where it is triggered by refering in the code (of stack). May be you declared the template class in a header file which doesnot have definitions (possibly you defined in LList.cpp) and included the header in stack.cpp. But template class requires the definitions also , so add the definitions in LList.cpp to the header itself.
Thanks for your response. That solved the problem completely. I created a LList.cpp file, which I didn't treat as a header, I added the template definitions to the header and it worked perfectly. Thanks again for your response! -Ken