Debugging
- gdb is a excellent command tool for debugging C/C++ code.
Syntax Mistakes
-
Missing "}". When this happens, you usually get lots of error message when you compile your code. And these error messages are often hard to understand and seems not related to your code.
-
Missing template arguments. This is relative easy to debug. The compiler will usually give clear enough error message.
-
Using member types of template types without the
typenameprefix. For example, instead oftemplate<class InputIt, class set<InputIt::value_type>> void f(...){ ... }
it should be
template<class InputIt, class set<typename InputIt::value_type>> void f(...){
...
}
The g++ compiler is usually smart enough to detect a missing typename and give you the right instruction
on how to fix the code.
- Accessing members in a template base class without using
thispointer. The compiler will tell you that these members are not found.
Logical Mistakes
- Abuse of
auto. Toughautoit a lot more convenient to work with template code, it is dangerous if an expression corresponds to several different types. For example, if you useauto x = "abc";
then x is of type const char * not std::string.
- Passing invalid iterators to functions/methods that operate on containers.
This usually result in segmentation fault.
For example,
if you use the
erasemethod of a vectorx(of length 10) to erase a range of elements from it, the second iterator must be no "smaller" than the first iterator. The codex.erase(x.begin()+1, x.begin());
will result in an error message of segmentation fault when you compile it.
- Forgeting to return a value for a non-void function/method. This usually results in segmentation fault.
Compile Option Mistakes
- Forgetting the
-lpthreadoption when compile code which uses the<thread>header. The compiler usually gives the following error message.terminate called after throwing an instance of 'std::system_error' what(): Unknown error 4294967295 Aborted