C++: error: need 'typename' before 'T::iterator' because 'T' is a dependent scope
Inside a template, a name like T::iterator could be a type or a value, so C++ requires the typename keyword to say it is a type.
need 'typename' beforebecause is a dependent scopemissing 'typename' prior to dependent type nameexpected a type, gotthere are no arguments to that depend on a template parameter Meaning
When the compiler parses a template it does not yet know what T is, so T::value_type is assumed to be a value. Prefix it with typename. The same idea applies to calling member templates, which need .template / ->template.
Common causes
- Using
T::iterator,T::value_typeorstd::vector<T>::const_iteratoras a type inside a template withouttypename - Calling a member template on a dependent object without
.template - Using a dependent base class member without
this->
⚡ Quick fix
- Write
typename T::iterator it = c.begin(); - Use
autofor iterators - Qualify base members with
this->orBase<T>::
Detailed fix by platform
C++
- In C++20,
typenameis optional in many contexts, but not all — add it when the compiler asks auto it = c.begin();avoids the problem completely
Code examples
typename in a template
#include <vector>
template <typename C>
void show(const C& c) {
// C::const_iterator it = c.begin(); // error: need 'typename' before 'C::const_iterator'
typename C::const_iterator it = c.begin(); // OK
for (; it != c.end(); ++it) { /* ... */ }
}
int main() { show(std::vector<int>{1, 2, 3}); }How to diagnose
- Template — Is the name inside a template?
- Kind — Is the dependent name a type?
- Base — Is the member from a dependent base class?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026