C++

C++ Invalid Operands Binary Operator Fix

A binary operator such as +, - or % accepts only certain pairs of operand types. When the pair is one the built-in operator does not define - two pointers added, a pointer multiplied, a double fed to %, a void result added - compilation stops with 'invalid operands of types ... to binary operator ...'. The operator is rarely the problem; the types on either side of it are. This guide separates the cases, reads what the diagnostic actually reports, and matches each one to its fix.

Written November 25, 2026 · Tested with GCC 13.3.0 · 9 min read

By the end of this guide
  • The rule underneath: each operator accepts a fixed set of operand pairs
  • How to read the diagnostic
  • The first failure: two pointers added together
  • The second failure: two string literals added together

Written November 25, 2026

Tested with GCC 13.3.0

What this guide is

Every built-in binary operator comes with a fixed menu of operand pairs it accepts. + accepts two numbers, or a pointer and an integer. % accepts two integers. When an expression hands an operator a pair that is not on its menu - two pointers for +, a double for %, a void result for + - compilation stops with "invalid operands of types '...' and '...' to binary 'operator...'". Neither operand is necessarily wrong on its own; the combination is what the operator does not define.

The causes in this guide each reproduce the failure with a small, complete program: two pointers added together, two string literals added together, an integer with a pointer subtracted from it, a floating-point operand to %, the result of a void call used in arithmetic, and nullptr itself used in arithmetic. In each family the failing program is small, the compiler output is shown, and the fix either changes the expression so the operands form a pair the operator accepts, or switches to the operation that actually does what the code meant.

Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs are compiled and run, so their output is shown.

This guide is about pairs of built-in types - pointers, arithmetic types, void, std::nullptr_t. When one operand is a class type such as std::string or your own struct and no operator+ is defined for it, GCC reports a different sentence, "no match for 'operator+'", instead. That wording is at the end, with the reason the two wordings split where they do. Shapes that look like they belong here but are legal - a pointer plus an integer, std::string concatenation, an unscoped enum in arithmetic - are also at the end, because mistaking them for this error leads to fixing code that was never broken.

The rule underneath: each operator accepts a fixed set of operand pairs

The built-in arithmetic operators are not one rule per operator symbol; they are a table of permitted operand combinations. For the additive operators, the built-in + accepts two arithmetic operands, or a pointer to a complete object type and an integer. The built-in - accepts those, and additionally two pointers to the same type, which yields the distance between them. Multiplication and division accept two arithmetic operands and nothing else. The remainder operator % accepts two integer operands only - a floating-point operand is not converted down to an integer to make the pair fit. An expression whose operand pair matches no row of the table has no built-in operator to call, and that is exactly what "invalid operands" reports.

Three consequences follow. First, the fix never changes the operator symbol in isolation; it changes one of the operands - its type, or what produces it - so the pair lands on a row of the table, or it replaces the expression with the library operation that does the intended job, such as std::fmod for a floating-point remainder. Second, the two operands are judged as a pair, in order. pointer + integer is a row of the table and integer + pointer is too, but integer - pointer is not, even though pointer - integer is: subtraction is not symmetric, and swapping the same two types around the operator can move an expression off the table. Third, conversions rescue some pairs and not others. Two arithmetic operands of different types, such as int and double, are brought to a common type by the usual arithmetic conversions, so mixed arithmetic is legal. There is no conversion that turns a second pointer into an integer offset, or a void result into a number, so those pairs stay invalid no matter what surrounds them.

That gives a fast way to read any instance of this error: read the two types named in the message, in order, then read the operator named at the end. Ask which row of that operator's table the code was aiming at, and which operand keeps the pair off it. The repair is almost always on that operand.

How to read the diagnostic

Every failure in the main families prints the same shape of output: the file and the enclosing function on the first line, then an error line giving the line and column of the operator, the full message naming the left operand's type, the right operand's type and the operator, and the source line with a diagram underneath that labels each operand with its type. Read it in order. The two types are stated in source order - left operand first - which matters for the non-symmetric operators: "'int' and 'int' to binary 'operator-'" and "'int' and 'int' to binary 'operator-'" are different situations, and only the second is legal. The diagram under the source line repeats the two types beneath the expressions that produced them, which is how you find the operand to change when the expression is not a plain variable - in the void family below, the diagram shows void sitting under the call, not under a variable. One family prints no diagram: the nullptr failure underlines the whole expression instead, because there is no pair of subexpressions to label separately. The wording is also stable across the families - the sentence is the same whether the rejected pair is two pointers or a pointer and an integer under * - so the sentence alone does not tell you which family you have; the two types and the operator at the end of it do.

The first failure: two pointers added together

A program that holds two pointers into one array and tries to add them:

C++
#include <iostream>
int main() {
    int a[4] = {10, 20, 30, 40};
    int* p = a;
    int* q = a + 2;
    auto r = p + q;
    std::cout << r << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_ptrplus.o fail_ptrplus.cpp
fail_ptrplus.cpp: In function ‘int main()’:
fail_ptrplus.cpp:6:16: error: invalid operands of types ‘int*’ and ‘int*’ to binary ‘operator+’
    6 |     auto r = p + q;
      |              ~ ^ ~
      |              |   |
      |              |   int*
      |              int*

Line 6 tries to add two addresses. Pointer arithmetic is defined as movement within an array: a pointer plus an integer moves that many elements. A pointer plus a pointer would be an address plus an address, which names no element of anything, so the pair is not on the table for +. What code in this shape almost always wants is one of two different things: the distance between the two positions, or the element one of them points at.

The distance is the other additive operation on two pointers - subtraction:

C++
#include <iostream>
int main() {
    int a[4] = {10, 20, 30, 40};
    int* p = a;
    int* q = a + 2;
    std::cout << (q - p) << "\n";
    std::cout << *q << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_ptrplus fix_ptrplus.cpp
$ ./fix_ptrplus
2
30

The output is 2, the number of elements between the two positions, then 30, the element q points at. If the intent was a position halfway or a position offset from both, compute the offset as an integer first - (q - p) / 2 - and add that integer to one pointer; an integer is the only thing that may be added to a pointer. What does not work is casting one pointer to an integer type and adding: the result is an integer holding a meaningless sum of two addresses, not a pointer to anything.

The second failure: two string literals added together

A program that tries to join two pieces of text with +:

C++
#include <iostream>
int main() {
    auto r = "abc" + "def";
    std::cout << r << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_literals.o fail_literals.cpp
fail_literals.cpp: In function ‘int main()’:
fail_literals.cpp:3:20: error: invalid operands of types ‘const char [4]’ and ‘const char [4]’ to binary ‘operator+’
    3 |     auto r = "abc" + "def";
      |              ~~~~~ ^ ~~~~~
      |              |       |
      |              |       const char [4]
      |              const char [4]

The types in the message give this family away: a string literal is not a string object, it is an array of characters, const char [4] here - three letters and the terminating null. In most expressions an array converts to a pointer to its first element, so the expression is the first family in disguise: two pointers, asked to add. There is no built-in concatenation for character arrays, because + on pointers is arithmetic, not joining.

Concatenation is a std::string operation. Making one operand a std::string puts the expression on the string operator+ overloads, which accept a string on either side of a character pointer:

C++
#include <iostream>
#include <string>
int main() {
    std::string r = std::string("abc") + "def";
    std::cout << r << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_literals fix_literals.cpp
$ ./fix_literals
abcdef

The output is abcdef. Only one side had to change: with a std::string on the left, the right-hand literal is accepted as the character pointer it converts to. The same applies in the other order - "abc" + std::string("def") is also one of the overloads. What does not work is leaving both sides as literals and hoping the compiler treats the expression as text; adjacent literals are joined by the compiler only when they are written next to each other with no operator at all, "abc" "def", which is a different mechanism and produces one literal, not a concatenation of two values.

The third failure: an integer with a pointer subtracted from it

A program that counts back from a pointer by subtracting in the wrong order:

C++
#include <iostream>
int main() {
    int a[4] = {10, 20, 30, 40};
    int* p = a + 3;
    auto back = 2 - p;
    std::cout << *back << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_intminusptr.o fail_intminusptr.cpp
fail_intminusptr.cpp: In function ‘int main()’:
fail_intminusptr.cpp:5:19: error: invalid operands of types ‘int’ and ‘int*’ to binary ‘operator-’
    5 |     auto back = 2 - p;
      |                 ~ ^ ~
      |                 |   |
      |                 int int*

Line 5 has the right two ingredients in the wrong order. Stepping back from a pointer is pointer - integer, a row of the table; integer - pointer asks what remains when an address is taken away from a count, which names nothing. This is the family where the order of the types in the message does the diagnostic work: the same two types with operator+ would compile in either order, because addition of a pointer and an integer is defined both ways, but subtraction is defined only with the pointer first.

Put the pointer first:

C++
#include <iostream>
int main() {
    int a[4] = {10, 20, 30, 40};
    int* p = a + 3;
    int* back = p - 2;
    std::cout << *back << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_intminusptr fix_intminusptr.cpp
$ ./fix_intminusptr
20

The output is 20: p points at 40, two elements back is 20. Note that the result type changed with the order, not just the legality - p - 2 is a pointer, so back is declared int* and dereferenced for printing. When you meet this family, do not reach for a cast: an expression that needs the operands swapped is a logic slip, and swapping them back states the intent the code already had.

The fourth failure: a floating-point operand to %

A program that tries to take the remainder of a division by a fractional value:

C++
#include <iostream>
int main() {
    double d = 3.5;
    auto r = 10 % d;
    std::cout << r << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_mod.o fail_mod.cpp
fail_mod.cpp: In function ‘int main()’:
fail_mod.cpp:4:17: error: invalid operands of types ‘int’ and ‘double’ to binary ‘operator%’
    4 |     auto r = 10 % d;
      |              ~~ ^ ~
      |              |    |
      |              int  double

The built-in % is the remainder of integer division, and its table has one row: two integer operands. A double operand is not rounded or truncated to fit - the usual arithmetic conversions, which rescue int + double elsewhere, do not apply here, because they would convert the int up to double, and a floating-point % is the thing that does not exist as a built-in. The bitwise operators report the same wording for the same reason: 6 & 2.5 fails as "'int' and 'double' to binary 'operator&'", because those operators also accept integer operands only.

The floating-point remainder is a library function, std::fmod, which computes the remainder of its two arguments as a floating-point value:

C++
#include <cmath>
#include <iostream>
int main() {
    double d = 3.5;
    double r = std::fmod(10.0, d);
    std::cout << r << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_mod fix_mod.cpp
$ ./fix_mod
3

The output is 3: 10 holds two whole 3.5s, 7, leaving 3. If the fractional part was never intended - the value arrived as a double from an earlier computation but is whole in practice - the other fix is to convert the operand to an integer type explicitly, with a cast at the % expression, and keep the built-in operator. Choose by intent: std::fmod when the remainder is genuinely fractional, a cast when the operand was an integer all along and its type, not its value, is what drifted.

The fifth failure: the result of a void call in arithmetic

A program that adds to the result of a call whose function returns nothing:

C++
#include <iostream>
void show() {
    std::cout << "hi\n";
}
int main() {
    auto total = show() + 1;
    std::cout << total << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_void.o fail_void.cpp
fail_void.cpp: In function ‘int main()’:
fail_void.cpp:6:25: error: invalid operands of types ‘void’ and ‘int’ to binary ‘operator+’
    6 |     auto total = show() + 1;
      |                  ~~~~~~ ^ ~
      |                      |    |
      |                      void int

The void type is the type with an empty set of values, so a call to a void function produces no value that an operator could consume. The diagram under the source line is what identifies this family: void sits under show(), the call, not under a variable. The code has usually drifted into this - the function once returned a count or a status and was later changed to print or store it instead, while a call site kept doing arithmetic on the old return value - or the author expected a print routine to also hand back something countable.

Separate the call from the arithmetic. The call runs as a statement for its effect, and the count lives in a variable the caller owns:

C++
#include <iostream>
void show() {
    std::cout << "hi\n";
}
int main() {
    show();
    int total = 0 + 1;
    std::cout << total << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_void fix_void.cpp
$ ./fix_void
hi
1

The output is hi, then 1. If the caller genuinely needs a number out of the function - how many lines it printed, whether it succeeded - that is the other fix: change the function's return type from void to the type of that number and return it, so the call site's arithmetic has a real operand. What does not work is casting the call to a numeric type; there is no value to convert, and the cast is rejected for the same reason the addition is.

The sixth failure: nullptr itself in arithmetic

A program that tries to offset from nullptr as if it were an address:

C++
#include <iostream>
int main() {
    auto r = nullptr + 1;
    std::cout << r << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o fail_nullptr.o fail_nullptr.cpp
fail_nullptr.cpp: In function ‘int main()’:
fail_nullptr.cpp:3:22: error: invalid operands of types ‘std::nullptr_t’ and ‘int’ to binary ‘operator+’
    3 |     auto r = nullptr + 1;
      |              ~~~~~~~~^~~

nullptr is a null pointer constant of its own type, std::nullptr_t. It converts to any pointer type, but it is not itself a pointer to storage, so there is no element one step past it and no arithmetic to perform. This family usually appears in code that treats nullptr as a starting address, or in a generic expression where a pointer argument was replaced by nullptr at one call and the arithmetic around it stayed. Note what the error is about: the type in the expression, not a null value. A pointer variable that currently holds nullptr compiles in the same p + 1 shape, because its type is int* - the arithmetic is legal for the type, and only dereferencing the result would be invalid, at run time rather than compile time.

Do the arithmetic on a pointer that points at something. Where the pointer may be null, test it first and give it a real target before offsetting:

C++
#include <iostream>
int main() {
    int a[3] = {10, 20, 30};
    int* p = nullptr;
    if (p == nullptr) {
        p = a;
    }
    int* r = p + 1;
    std::cout << *r << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o fix_nullptr fix_nullptr.cpp
$ ./fix_nullptr
20

The output is 20, the second element. The comparison p == nullptr is the sanctioned use of nullptr next to a pointer - equality is defined for it, arithmetic is not. If the null case has no fallback target, the fix is a branch that handles it as an error or an early exit, not an offset: there is no "one past nothing" to point at.

The fixes in reach order

  • Two pointers under +, * or /: the operator is the wrong tool. For a distance, subtract the pointers; for a position, add an integer offset to one pointer; for an element, dereference.
  • Two string literals under +: make one side a std::string - the string overloads then accept the other side as a character pointer or a character.
  • An integer and a pointer under -, in that order: swap them. Pointer first, offset second; check whether the result should be a pointer or, from pointer minus pointer, a distance.
  • A floating-point type named with % or a bitwise operator: if the remainder is genuinely fractional, use std::fmod; if the operand was meant to be an integer, convert it explicitly at the expression.
  • void named as an operand type: find the call under it in the diagram. Run the call as a statement and keep the number in a variable, or change the function's return type so the call produces the value the arithmetic needs.
  • std::nullptr_t named as an operand type: the expression is doing arithmetic on nullptr itself. Point a real pointer at real storage first, or branch on the null case; use nullptr for comparisons, not offsets.
  • A class type is involved and the wording is "no match for 'operator...'": that is the neighboring problem - see the traps. No overload exists for that pair; the repair is a conversion, a different library operation, or defining the operator.

Traps

  • A pointer plus an integer, and a pointer minus a pointer, are both legal - the two shapes the first family is most often confused with. The integer is an element count, and the pointer difference is an element count:
C++
#include <iostream>
int main() {
    int a[4] = {10, 20, 30, 40};
    int* p = a + 1;
    int* q = p + 2;
    std::cout << *q << "\n";
    std::cout << (q - p) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o trap_ptrarith trap_ptrarith.cpp
$ ./trap_ptrarith
40
2
Trap
The output is 40, the element two past p, then 2, the distance back. An array name in these expressions converts to a pointer to its first element, which is why a + 1 is the same legal shape. The line that separates this from the first family is the second operand's type: integer, legal; pointer, invalid. - std::string concatenation is legal in every direction a beginner tries, because the library defines the overloads: string plus character pointer, string plus a single character, string plus string:
C++
#include <iostream>
#include <string>
int main() {
    std::string s = "abc";
    std::cout << (s + "def") << "\n";
    std::cout << (s + 'd') << "\n";
    std::cout << (s + std::string("efg")) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o trap_stringcat trap_stringcat.cpp
$ ./trap_stringcat
abcdef
abcd
abcefg

The output is the three concatenations. Compare the second family: the failing expression had no std::string on either side. When a + between text fails, look for whether any operand is actually a std::string before changing anything - if one is, the problem is elsewhere, usually the other operand's type. - An unscoped enum value in arithmetic is legal, because the value converts to an integer type before the operator sees it:

C++
#include <iostream>
enum Color { red, green, blue };
int main() {
    Color c = green;
    std::cout << (c + 1) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o trap_enum trap_enum.cpp
$ ./trap_enum
2

The output is 2, from green converting to 1. A scoped enum - enum class - does not convert, and the same expression with one reports "no match for 'operator+'", the neighboring wording below, not "invalid operands". Which enum kind the code declares decides which of the three outcomes - legal, no match, or something else - the same-looking line gets. - Mixed int and double arithmetic is legal. The usual arithmetic conversions bring both operands to a common type, double here, before the operator runs:

C++
#include <iostream>
int main() {
    std::cout << (1 + 2.5) << "\n";
    std::cout << (10 / 4.0) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o trap_promotion trap_promotion.cpp
$ ./trap_promotion
3.5
2.5

The output is 3.5, then 2.5. This is the contrast that makes the fourth family precise: conversions rescue mixed arithmetic for + and /, but they do not create a floating-point %, because the operator itself has no floating-point row for the converted pair to land on. - A class type with a defined operator is legal, and defining the operator is the fix route for your own types. With an operator+ written for the struct, the addition the compiler otherwise rejects compiles and runs:

C++
#include <iostream>
struct Point {
    int x;
    int y;
};
Point operator+(const Point& a, const Point& b) {
    return Point{a.x + b.x, a.y + b.y};
}
int main() {
    Point p{1, 2};
    Point q{10, 20};
    Point r = p + q;
    std::cout << r.x << " " << r.y << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -o trap_userop trap_userop.cpp
$ ./trap_userop
11 22

The output is 11 22, the component-wise sum. Without the operator+ definition, p + q reports "no match for 'operator+'" - which is the correct prompt to write this definition, when adding two values of the type is a meaningful operation. It is not a route for the built-in families above: no definition you write changes what int* + int* or 10 % 3.5 mean. - A class-type operand with no matching overload reports "no match for 'operator...'", not "invalid operands" - the wording split is systematic, and reading it tells you which repair class you are in. This program repeats text by multiplication, an operation std::string does not define:

C++
#include <string>
int main() {
    std::string s = "ab";
    auto r = s * 3;
    return 0;
}
Terminal
$ g++ -std=c++20 -Wall -Wextra -c -o trap_nomatch.o trap_nomatch.cpp
trap_nomatch.cpp: In function ‘int main()’:
trap_nomatch.cpp:4:16: error: no match for ‘operator*’ (operand types are ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} and ‘int’)
    4 |     auto r = s * 3;
      |              ~ ^ ~
      |              |   |
      |              |   int
      |              std::string {aka std::__cxx11::basic_string<char>}

The same split holds for std::string minus a std::string, a std::vector plus a std::vector, and your own struct plus an int: when at least one operand is a class type, GCC looks for an overload, finds none that fits, and says so, often followed by a list of the candidates it rejected. "Invalid operands" is reserved for pairs of built-in types, where the built-in operator exists but the pair is not one it accepts. The repairs differ accordingly: a "no match" asks for an overload, a conversion, or a library operation such as a loop or an algorithm; an "invalid operands" asks which operand's type or position breaks the built-in operator's table.

Checklist

  • The two operand types and the operator have been read from the message, in source order, and the operand each belongs to has been found in the expression - in the source line or in the diagram under it.
  • For a pointer pair: the intent is settled - distance (pointer minus pointer), position (pointer plus or minus an integer), or element (dereference) - and the expression rewritten to that operation.
  • For text: at least one operand is a std::string wherever + is meant to concatenate, and no + joins two bare literals.
  • For - with a pointer involved: the pointer is the left operand, or both operands are pointers and a distance is what is wanted.
  • For % or a bitwise operator: both operands are integer types, or the operation has been replaced - std::fmod for a fractional remainder.
  • For a void operand: the call producing it either runs as a statement with the value kept elsewhere, or the function's return type now produces the value.
  • For std::nullptr_t: no arithmetic is done on nullptr itself; a pointer is compared against nullptr and only offset after it points at storage.
  • The wording has been checked: "no match for 'operator...'" has been treated as the overload problem it is, not as this one.
  • A legal shape - pointer plus integer, pointer minus pointer, string concatenation, unscoped enum arithmetic, mixed arithmetic, a defined operator - has been left alone.
  • The fixed program run, and its output checked against what the program was meant to do.

Sources

  • cppreference, Arithmetic operators - for the operand combinations the built-in additive, multiplicative and remainder operators accept, for pointer-plus-integer and pointer-minus-pointer arithmetic within an array, and for % requiring integer operands.
  • cppreference, std::basic_string operator+ - for the concatenation overloads accepting a string with another string, a character pointer, or a single character, on either side.
  • cppreference, nullptr - for nullptr being a null pointer constant of type std::nullptr_t, convertible to any pointer type, and usable in comparisons against pointers.
  • cppreference, std::fmod - for fmod computing the floating-point remainder of its two arguments.
  • cppreference, Fundamental types - for void being the type with an empty set of values, so a call to a void function yields no value an operator can consume.

Keep Exploring