Compiler and linker errors

C++ “no matching function for call to”: how to read the error

The compiler reports “no matching function for call to” when none of the visible functions with that name accepts the arguments at the call site. Compare the argument count and types with each candidate listed below the error. A function can exist and still fail to match because a reference cannot bind or a conversion is not allowed.

With a single, non-overloaded function, GCC usually reports a more specific message, such as “too many arguments to function” or “cannot convert.” GCC uses “no matching function for call to” when it compares several candidates, including overloads, constructors, and function templates. Clang reports “no matching function for call to” in both situations.

Read the candidate notes

A candidate is a function the compiler considered for a call. The diagnostic often prints each candidate's parameter list and explains why an argument does not fit. Read those notes after the first error line. They can show that a function accepts one argument when the call supplies two, or that the call passes a value with a different type.

Do not fix the message by adding arbitrary overloads. First identify the intended operation. Then make the call match an existing signature, or change the signature only if the function really needs different inputs. A matching function must also be declared before its use and visible in the correct namespace.

Check argument count and types

The number of arguments at a call must fit a candidate's parameter list, including any default arguments. A default argument can omit a trailing argument, but it cannot accept an extra one. When the count matches, compare each argument from left to right with the corresponding parameter type.

Some conversions are permitted, such as converting an integer to a floating-point value for a parameter. Other conversions require an explicit constructor or cast. Avoid adding casts only to silence the error: confirm that the converted value has the meaning the function expects.

Failing example: neither overload matches

#include <iostream>
#include <string>

void showCount(int count) {
    std::cout << "items=" << count << '\n';
}

void showCount(int count, const std::string& label) {
    std::cout << label << "=" << count << '\n';
}

int main() {
    showCount(3, 4);
    return 0;
}

GCC diagnostic core:

error: no matching function for call to

The exact wording and candidate notes vary by compiler and version. MSVC reports C2665 when none of the overloads can convert all the argument types. The one-argument overload has too few parameters, and the two-argument overload expects a string for its second parameter.

Fixed example

#include <iostream>
#include <string>

void showCount(int count) {
    std::cout << "items=" << count << '\n';
}

void showCount(int count, const std::string& label) {
    std::cout << label << "=" << count << '\n';
}

int main() {
    showCount(4);
    return 0;
}

Expected output:

items=4

Understand const and references

A non-const lvalue reference such as std::string& refers to a named, modifiable object. It cannot bind to a temporary value because the function signature promises that the referred-to object can be changed. A const reference can bind to a temporary and read it for the duration of the call. Use a value parameter when the function needs its own copy.

Constness also affects overload selection. A function taking const T& can accept a const or non-const object for reading. A function taking T& requires a modifiable lvalue. Passing a const object to a function that promises to modify it is not safe, so the compiler rejects that candidate.

Failing example: a temporary cannot bind to a non-const reference

#include <iostream>
#include <string>

void printLabel(std::string& label) {
    std::cout << label << '\n';
}

int main() {
    printLabel(std::string("parcel"));
    return 0;
}

GCC diagnostic core for example 2:

cannot bind non-const lvalue reference

The exact wording varies by compiler and version. MSVC in its standard conformance mode (/permissive-, the default for new Visual Studio projects) reports C2664. The temporary string is not a modifiable named object that a non-const reference can represent.

Fixed example

#include <iostream>
#include <string>

void printLabel(const std::string& label) {
    std::cout << label << '\n';
}

int main() {
    printLabel(std::string("parcel"));
    return 0;
}

Expected output:

parcel

If a diagnostic lists several overload candidates, inspect each rejection reason instead of focusing only on the function name. Check that the header declares the overload you intended, then check its namespace, parameter order, and const qualifiers. Avoid changing a parameter to a broader type when a call-site typo is the real problem.

Exercise

Fix the broken call so it prints a product label and quantity. The overloads accept an integer and a string, or two strings. The call supplies a string and an integer, so neither overload matches. Sample input:

notebook 2

Failing exercise

#include <iostream>
#include <string>

void printProduct(int quantity, const std::string& label) {
    std::cout << label << ": " << quantity << '\n';
}

void printProduct(const std::string& label, const std::string& category) {
    std::cout << category << ": " << label << '\n';
}

int main() {
    std::string label;
    int quantity = 0;
    if (!(std::cin >> label >> quantity)) {
        return 1;
    }
    printProduct(label, quantity);
    return 0;
}

GCC diagnostic core for example 3:

error: no matching function for call to

The exact wording varies by compiler and version. MSVC reports C2665 when none of the overloads can convert all the argument types. Expected output after the fix:

notebook: 2

Hint 1: Compare each call argument with the parameter in the same position. Hint 2: Pass the quantity before the label.

Show one possible solution
#include <iostream>
#include <string>

void printProduct(int quantity, const std::string& label) {
    std::cout << label << ": " << quantity << '\n';
}

void printProduct(const std::string& label, const std::string& category) {
    std::cout << category << ": " << label << '\n';
}

int main() {
    std::string label;
    int quantity = 0;
    if (!(std::cin >> label >> quantity)) {
        return 1;
    }
    printProduct(quantity, label);
    return 0;
}

Keep learning with CppPath

Continue with the Function Parameters & Return Values lesson. The full lesson includes a quiz and a checked coding challenge with a free account.