# C++: error: no match for 'operator<<' (operand types are 'std::ostream' and 'X')

**Code:** `no match for 'operator'` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

You used an operator on a type that does not define it — most often printing your own class with `<<` or comparing two objects.

## Meaning

C++ does not generate `operator<<`, `==` or `<` for your classes. The long list of "candidate" overloads that follows the message shows the closest built-in matches; none accept your type. Provide the operator as a free function (or member) and make sure it is visible where you use it.

## Common causes

- Class has no `operator<<` for std::ostream
- Class has no operator== / operator< (needed by std::find, std::sort, std::map)
- Operator declared in another namespace, so ADL cannot find it
- const mismatch: operator takes a non-const reference
- Missing `#include <iostream>` / `<string>`

## Quick fix

1. Define the operator for your type
2. Put the operator in the same namespace as the class
3. Take parameters by const reference

## Detailed fix by platform

### C++

1. Declare `std::ostream& operator<<(std::ostream& os, const Point& p);` next to the class
2. In C++20 you can write `bool operator==(const Point&) const = default;` or `auto operator<=>(const Point&) const = default;`

## Code examples

### Teach std::cout to print a class

```cpp
#include <iostream>

struct Point { int x, y; };

std::ostream& operator<<(std::ostream& os, const Point& p) {
    return os << "(" << p.x << ", " << p.y << ")";
}

int main() {
    Point p{1, 2};
    std::cout << p << "\n";     // prints (1, 2)
}
```

## How to diagnose

1. **Types** — Operand types in the message?
2. **Namespace** — Is the operator in the class's namespace?
3. **Const** — Does the signature use const T&?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/no-match-for-operator)
