-3

I'm using templates while implementing AVL trees on Ubuntu.

The file will not compile when I write template class AVLTree<std::list<int> >;, it tells me:

undefined reference to `AVLTree < std::__cxx11::list < std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> >, std::allocator < std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> > > > >::insert(std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> >)'

And I don't get what it doesn't have the reference to.

But it compiles just fine when I write template class AVLTree<std::list<string> >;

I need to let AVLTree store linked lists that store string values.

Why does one compile and the other doesn't? How to solve my problem?

PS: I've included <list>, <string>, and <iostream>, along with my own header file.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
WannabeArchitect
  • 1,058
  • 2
  • 11
  • 22
  • `difference between std::list and std::list in C++`. The difference is they 're different. – DimChtz Nov 11 '17 at 02:53
  • 2
    @DimChtz The question is poorly phrased. It should be "Why does `std::list` work but not `std::list`?" – Raymond Chen Nov 11 '17 at 02:54
  • Besides the obvious that they are different types, the problem leading to the error is impossible to guess without a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Nov 11 '17 at 02:54
  • @RaymondChen Thank you. I've changed my title. – WannabeArchitect Nov 11 '17 at 02:58
  • @Someprogrammerdude I'd love to make my question better. I've read the Minimal, Complete, and Verifiable Example part, but which part do I need to fix? Thanks. – WannabeArchitect Nov 11 '17 at 03:04
  • Your question is not verifiable or complete. There is not enough information available for somebody else to reproduce the problem. You didn't post any code at all. Further, `AVLTree` is not a standard type, so nobody knows what it is. We know what ``, `` and `` are, but we don't know what your custom headerfile is. – Raymond Chen Nov 11 '17 at 03:09
  • @RaymondChen Right. But it's a violation of my course's rule to post full codes. Since AVL trees are a very well-known data structures, I thought it will be enough to mention that I'm using templates. By headerfile, I meant "avl_tree.h" which I couldn't post for the same reason. I thought it wouldn;t be necessary, but I guess I was wrong. – WannabeArchitect Nov 11 '17 at 03:14
  • 1
    Your question doesn't have **nearly** enough information to be a valid question here. Anyway, you are linking together pieces of code built with different definitions of `std::string`. Compile with `-D_GLIBCXX_USE_CXX11_ABI=0` so you always get the old version. – Jonathan Wakely Nov 11 '17 at 03:15
  • You are actually quite lucky that someone managed to guess from your error message. I'm kind of impressed with their ability to infer the correct answer from such little information. You should've at the very least posted the code that generated the error, even if you didn't post the code for `AVLTree` itself. – Omnifarious Nov 11 '17 at 11:16

1 Answers1

1

Examining the error message closely shows that linker cannot find the AVLTree::insert(string) method.

Based on the sparse information that you posted, my best hypothesis is that you changed the template parameter in the following line from list<string> to list<int>:

template class AVLTree<std::list<string>>;

This line of code explicitly tells the compiler to instantiate a version of the AVLTree template using list<string> as the template parameter. Thus, when you try to compile the code after the change it gives you the error message that it cannot find the AVLTree::insert(string) function because the compiler is now generating the code for list<int> instead.

Your program contains other code that is referencing AVLTree<list<string>>. At a minimum you will have to update that code to use list<int> as well.

Plus, if you simplify the problem down to something you can post the code for on this site, then you will either find the issue during that process or at least have a change of getting a good answer.

RandomBits
  • 4,194
  • 1
  • 17
  • 30