Learn

Fixing "no matching function for call to" in Arduino

Beginner

Or see every lesson.

  • 455parts on the bench
  • 43boards running now
  • 1.00xreal time, on every board
Arduino Uno: a DHT11, made with the two values its library wantslive0.000 s 0.00x
Click to open it in the editor
Delete DHT11 from the line that makes the sensor and the build stops with this error.

no matching function for call to 'pinMode' means a function was called with values that do not fit any version of it. Either there are too few or too many, or one is the wrong kind: an array where a number goes, a String where C text goes, a number where a variable has to be. The notes under the error list every version that exists and say why each one did not fit. Read the first note; it usually names the problem.

The circuit above is a DHT11 on an Uno that prints "temperature 22 C, humidity 45 %". Open it, change DHT dht(2, DHT11); to DHT dht(2); and press Run. The build stops with no matching constructor for initialization of 'DHT', the note says requires at least 2 arguments, but 1 was provided, and the build tab explains: "A DHT is made here with values it does not take." The DHT library needs the sensor type as well as the pin.

Below is each way it happens, with clang’s words, the Arduino IDE’s GCC words, which are often different, and the fix.

A library object made with the wrong values

An object is made by its constructor, and a constructor takes what its library says. Here is the line from the sketch running above. DHT dht(2); gave clang’s no matching constructor for initialization of 'DHT', with a note for each constructor the library has. GCC calls a constructor a function, so the Arduino IDE says no matching function for call to 'DHT::DHT(int)' and candidate expects 3 arguments, 1 provided; the third has a default, so two are enough.

Open an example that came with the library and copy how it makes the object. A sketch from a tutorial may have been written for another library with the same header name, or another version, which is also the story behind no matching function for call to 'LiquidCrystal_I2C::begin()': one LiquidCrystal_I2C wants begin(16, 2) and another begin(). Mokxi’s takes both, and the has-no-member-named page tells the rest.

firmware/uno/examples/kitdht11 (the line that makes the sensor)
#include <DHT.h>

DHT dht(2, DHT11);

Too few or too many values

pinMode(13); stopped with no matching function for call to 'pinMode' and the note candidate function not viable: requires 2 arguments, but 1 was provided. GCC words the same mistake differently: too few arguments to function 'void pinMode(uint8_t, uint8_t)'. delay(1000, 2); gave requires single argument 'ms', but 2 arguments were provided, and GCC too many arguments to function 'void delay(long unsigned int)'.

Your own functions work the same way. blinkLed(13) on a blinkLed(int pin, int ms) gave no matching function for call to 'blinkLed', where GCC says too few arguments to function 'void blinkLed(int, int)'. Either pass both, or give the second a default in the definition, so it can be left out. The sketch below called it both ways and printed "blinked twice".

A default value for the second parameter
void blinkLed(int pin, int ms = 200) {
  digitalWrite(pin, HIGH);
  delay(ms);
  digitalWrite(pin, LOW);
  delay(ms);
}

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  blinkLed(13);
  blinkLed(13, 500);
  Serial.println("blinked twice");
}

void loop() {
}

Serial.println with two values

Serial.println("Temp: ", t); is the most common one. println prints one thing; its second value, when there is one, is how to print the first: the base, as in println(n, HEX), or the decimal places, as in println(t, 1). So clang said no matching member function for call to 'println' and listed all fourteen versions, the first ones with no known conversion from 'const char[7]' to 'int' for 1st argument. Print the label and the value in two calls; with t at 21.5 that printed "Temp: 21.5".

Three values cannot fit at all: Serial.println(a, b, c); gave requires at most 2 arguments, but 3 were provided for each version, and GCC no matching function for call to 'HardwareSerial::println(int&, int&, int&)'. Use one print() for each value, or build the line with snprintf first.

A label and a value, in two calls
void setup() {
  Serial.begin(9600);
  float t = 21.5;
  Serial.print("Temp: ");
  Serial.println(t, 1);
}

void loop() {
}

The right number of values, of the wrong kind

An array where one pin goes: pinMode(leds, OUTPUT); with int leds[] = {11, 12, 13}; gave no known conversion from 'int[3]' to 'uint8_t' (aka 'unsigned char') for 1st argument. GCC says invalid conversion from 'int*' to 'uint8_t'. pinMode() sets one pin, so loop over the array; the sketch below printed "three outputs". A pin number in quotes, pinMode("13", OUTPUT), fails the same way, from 'const char[3]'.

A String where C text goes: strcmp(cmd, "on") with cmd a String gave no known conversion from 'String' to 'const char *' for 1st argument, GCC says cannot convert 'String' to 'const char*', and the build tab says strcmp takes C text (a char array or a "quoted" string), not a String. Write cmd.c_str(), or compare Strings directly with cmd == "on".

A plain number where a variable goes: with void addOne(int &n), addOne(5) gave expects an lvalue for 1st argument. The function changes its argument, and 5 cannot be changed. Pass a variable. GCC says cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'.

One pinMode() per pin
int leds[] = {11, 12, 13};

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 3; i++) {
    pinMode(leds[i], OUTPUT);
  }
  Serial.println("three outputs");
}

void loop() {
}

A member function called with nothing

arm.attach(); on a Servo stopped with no matching member function for call to 'attach': one version requires single argument 'pin', but no arguments were provided, and the other requires 3. GCC says no matching function for call to 'Servo::attach()'. clang says member function when the call has a dot in it, so search for both wordings. The fix is the pin: arm.attach(9);.

Two functions of your own with one name, such as show(int) and show(float), are overloads, and show("hi") matched neither: no known conversion from 'const char[3]' to 'int', then to 'float'. Add a version for text, or pass a number.

Questions

What does "no matching function for call to" mean in Arduino?

A function was called with values that fit none of its versions: too few, too many or the wrong kind. The notes under the error list each version and why it did not fit.

Why does my library give "no matching function for call to" when the tutorial works?

The tutorial was written for a different library with the same name, or a different version, whose functions take other values. Use the installed library’s own examples, or install the one the tutorial used.

How do I fix "no matching function for call to DHT::DHT(int)"?

Pass the sensor type as well as the pin: DHT dht(2, DHT11); or DHT22 for that sensor.

Is "too few arguments to function" the same error?

Yes. GCC, the Arduino IDE’s compiler, says too few or too many arguments when there is only one version of the function; clang says no matching function for call to with a note. The fix is the same: match the values to the function.

Why can’t I print a label and a value with one Serial.println?

println takes one thing to print; a second value is the base or the number of decimals. Use Serial.print() for the label and Serial.println() for the value.

Sources

Where the facts on this page come from

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.