Skip to content
C++ Better Explained
Go back
How to Fix 'no matching function for call to' in C++

How to Fix “no matching function for call to” in C++

Short answer: you called a function with arguments that don’t fit any of its versions. It almost always involves a function with several versions: an overloaded function, a constructor, or a standard library function like push_back, getline or std::max. The candidate notes printed under the error list every version the compiler tried and why each one didn’t fit. Read those first.

CompilerWhat you see
GCC (g++, MinGW, Code::Blocks)no matching function for call to 'add(int)'
Clang (Xcode, macOS)no matching function for call to 'add'
Visual StudioC2661, C2665, C2672, or C2512 for constructors

Hit “no matching function for call to”? It's a classic beginner trap, and there are others like it.

Free 1-page checklist: The 10 Mistakes Every C++ Beginner Makes. It shows the exact traps that slow down every C++ beginner, so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


How to Read the Error

The message comes in two parts:

  1. The first line shows the call you made, with the types of your arguments: no matching function for call to 'add(int)'.
  2. The candidate notes underneath list each version that exists, and why it didn’t match: candidate expects 2 arguments, 1 provided.

Find the version you meant to call, and compare its parameters with what you passed. The mismatch is almost always visible right there.

A side note: GCC only says “no matching function” when there’s more than one version to choose from. Call a function that has a single version with the wrong arguments, and you’ll get too few arguments to function or could not convert instead. Same problem, plainer wording.

Cause 1: Wrong Number of Arguments

#include <iostream>

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

int main() {
    std::cout << add(5) << '\n';   // neither version takes one argument
}
GCC:    error: no matching function for call to 'add(int)'
        note:   candidate expects 2 arguments, 1 provided
Clang:  error: no matching function for call to 'add'
        note: candidate function not viable: requires 2 arguments, but 1 was provided
MSVC:   error C2661: 'add': no overloaded function takes 1 arguments

Fix: pass what one of the versions expects, such as add(5, 3). See function overloading for how C++ picks between versions.

Cause 2: No Matching Constructor

The most common version of this error in object-oriented code:

#include <iostream>

class Point {
public:
    Point(int x, int y) : x(x), y(y) {}
    int x, y;
};

int main() {
    Point origin;   // there is no constructor that takes no arguments
    std::cout << origin.x << '\n';
}
GCC:    error: no matching function for call to 'Point::Point()'
        note: candidate: 'Point::Point(int, int)'
Clang:  error: no matching constructor for initialization of 'Point'
MSVC:   error C2512: 'Point': no appropriate default constructor available

Point origin; asks for a constructor with no arguments. The moment you write any constructor yourself, C++ stops generating that default one for you.

Fix: pass the arguments the constructor needs — Point origin(0, 0); — or add a default constructor:

#include <iostream>

class Point {
public:
    Point() : x(0), y(0) {}               // add a default constructor...
    Point(int x, int y) : x(x), y(y) {}
    int x, y;
};

int main() {
    Point origin;        // ...and this works
    Point p(3, 4);
    std::cout << origin.x << ' ' << p.y << '\n';
}

More in constructors and destructors.

Want the fundamentals explained properly? C++ Better Explained covers the core of C++ in plain English, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.

Get the book — $19

Cause 3: The Wrong Type for push_back

#include <vector>

int main() {
    std::vector<int> scores;
    scores.push_back("ninety");   // a string, but the vector holds ints
}
GCC:    error: no matching function for call to 'push_back(const char [7])'
Clang:  error: no matching member function for call to 'push_back'
        note: candidate function not viable: no known conversion from 'const char[7]' to 'const value_type' (aka 'const int') for 1st argument
MSVC:   error C2665: 'std::vector<int,std::allocator<int>>::push_back': no overloaded function could convert all the argument types

const char [7] is the type of the text "ninety". The vector only accepts ints.

Fix: push the right type — scores.push_back(90); — or use a std::vector<std::string> if you really want to store text.

Cause 4: getline Into a Number

A classic when reading input:

#include <iostream>
#include <string>

int main() {
    int age;
    std::getline(std::cin, age);   // getline reads into a std::string
}
GCC:    error: no matching function for call to 'getline(std::istream&, int&)'
Clang:  error: no matching function for call to 'getline'
MSVC:   error C2672: 'std::getline': no matching overloaded function found

std::getline reads a whole line of text, so it needs a std::string.

Fix: read into a string, then convert it — or use std::cin >> age; if you only want the number:

#include <iostream>
#include <string>

int main() {
    std::string line;
    std::getline(std::cin, line);    // read the whole line as text...
    int age = std::stoi(line);       // ...then convert it
    std::cout << age + 1 << '\n';
}

See getline vs cin for when to use each.

Cause 5: Mixed Types in std::max and std::min

#include <algorithm>
#include <iostream>

int main() {
    int a = 5;
    double b = 2.5;
    std::cout << std::max(a, b) << '\n';   // int and double
}
GCC:    error: no matching function for call to 'max(int&, double&)'
        note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'double')
Clang:  error: no matching function for call to 'max'
        note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('int' vs. 'double')
MSVC:   error C2672: 'std::max': no matching overloaded function found

std::max compares two values of the same type. Given an int and a double, it can’t decide which type to use, so neither version matches.

Fix: tell it the type:

#include <algorithm>
#include <iostream>

int main() {
    int a = 5;
    double b = 2.5;
    std::cout << std::max<double>(a, b) << '\n';   // tell max which type to use
}

Or make both arguments the same type first, for example with static_cast<double>(a).

Quick Reference

You seeUsual causeFix
candidate expects 2 arguments, 1 providedWrong number of argumentsPass what one version expects
no matching function for call to 'X::X()'No default constructorPass arguments, or add X()
push_back(const char [N])Wrong element typePush the vector’s element type
getline(std::istream&, int&)getline into a numberRead a std::string, then convert
deduced conflicting typesMixed types in a templateName the type, or convert one argument

For other common messages, see C++ error messages explained.


Take Your C++ Further

C++ Better Explained ebook cover

If you'd like the fundamentals explained in one place, C++ Better Explained covers them in 87 plain-English pages, with analogies and code diagrams, including the parts that trip beginners up most: pointers, memory and OOP.

Get the book — $19

87 pages · plain English · 30-day money-back guarantee


📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
How to Fix 'does not name a type' in C++
Next Post
C++ Primer vs C++ Primer Plus: Which Should You Buy?

Keep Learning