# C++: error: need 'typename' before 'T::iterator' because 'T' is a dependent scope

**Code:** `need typename before dependent name` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

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.

## 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_type` or `std::vector<T>::const_iterator` as a type inside a template without `typename`
- Calling a member template on a dependent object without `.template`
- Using a dependent base class member without `this->`

## Quick fix

1. Write `typename T::iterator it = c.begin();`
2. Use `auto` for iterators
3. Qualify base members with `this->` or `Base<T>::`

## Detailed fix by platform

### C++

1. In C++20, `typename` is optional in many contexts, but not all — add it when the compiler asks
2. `auto it = c.begin();` avoids the problem completely

## Code examples

### typename in a template

```cpp
#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

1. **Template** — Is the name inside a template?
2. **Kind** — Is the dependent name a type?
3. **Base** — Is the member from a dependent base class?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/dependent-name-needs-typename)
