The University of Queensland Homepage
School of ITEE ITEE Main Website

 Using STL features like string and list

Introduction

GCC and other modern C++ compilers often provide support for the Standard Template Library (STL). This provides containers like list and vector, and convenient functionality like strings.

Unfortunately, when using some of these in combination, some implementations emit errors such as the following:

/opt/local/gnu/lib/g++-include/std/sinst.h:64:
ambiguous template instantiation for
`operator >=(const basic_string > &, const char *)' requested
These errors come from just #including the required header files!

The Workaround

The workaround seems to be to #include <string> before container classes, e.g. #include <list>. In a utility class, which may well be used by other files, id you need to use say <list >, but not <string>, then it would be wise to include string anyway, just before list. For example: #include <stdio.h> #include <string> // Not needed, but this solves problems! #include <list> // For list<mytype> This won't cause problems later on because header files like <string> have #ifdef protection against being included twice. By including <string> even before you need it, you ensure that even if some other file that includes your file needs <string> and includes it after your include file, the compiler will see <string> before <list>, and all will be well.