Incompatible operand types when using ternary conditional operator


Andrey Chernukha

this code:

  bool contains = std::find(indexes.begin(), indexes.end(), i) != indexes.end();
  CardAbility* cardAbility = contains ? new CardAbilityBurn(i) : new CardAbilityEmpty;

gives me the following error:

Incompatible operand types CardAbilityBurn and CardAbilityEmpty

However, if I write the code like this:

 if (contains)
 {
    cardAbility = new CardAbilityBurn(i);
 }
 else
 {
    cardAbility = new CardAbilityEmpty;
 }

Then the compiler doesn't care. why is it like this? I want to use the ternary conditional operator because it's only one line. What's up

I need to point out (I think you might need this info), CardAbilityEmptyandCardAbilityBurn both stem from CardAbilitysiblings.

thanks

Quentin

C++'s type system determines the type of an expression from the inside out [1] . This means that the type of the conditional expression is determined before assignment to to , and the compiler must choose between only andCardAbility*CardAbilityBurn*CardAbilityEmpty* .

Since C++ has multiple inheritance and more possible conversion paths, since all types are not superclasses of other types, the compilation ends here.

In order to compile successfully, you need to provide the missing part: cast one or both operands to a base class type, so the entire conditional expression can take that type.

auto* cardAbility = contains
    ? static_cast<CardAbility*>(new CardAbilityBurn(i))
    : static_cast<CardAbility*>(new CardAbilityEmpty  );

(Note the use of auto, since you already provided the target type in the right-hand side expression.)

It 's but a bit puzzling, so in the end if-else the structure is better suited in this case.

[1] There is one exception: overloaded function names do not have a definite type until they are (implicitly or explicitly) converted to one of their versions.

Related


Incompatible types in ternary conditional operator

mangala I'm trying to set a custom renderer for a dropdown box based on a certain condition: themeComboBox.setRenderer( settings == null ? themeComboBox.getRenderer() : new ThemeNameRenderer()); where themeComboBoxis an instance javax.swing.JComboBox<Path

instanceof - incompatible conditional operand types

java_geek: The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this is not: String s = new String(); System.out.println(s instanceof Cloneable); Raises a compiler error. what is the problem? Polygenic

instanceof - incompatible conditional operand types

java_geek: The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this is not: String s = new String(); System.out.println(s instanceof Cloneable); Raises a compiler error. what is the problem? Polygenic

instanceof - incompatible conditional operand types

java_geek: The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this is not: String s = new String(); System.out.println(s instanceof Cloneable); Raises a compiler error. What's the question? Polygenic

Ternary conditional operator for left operand in C#

Andrei Dryazgov Is it possible to select a target variable based on some inline condition without an if statement? (!RTL ? padLeft : padRight) = NearBorder.LineWidth; Pinkfloydx33 If you are using a sufficiently new version of C# (7.2+) and these variables ar

TypeScript union types with conditional (ternary) operator usage

Benjamin Vincent I'm getting a cryptic error message when using two custom Typecsipt types (temporary name sorry) and the ternary operator, no error message when I use only 'OptionsPostData3 | OptionsPostData4'or only OptionsPostData3half or OptionsPostData4th

TypeScript union types with conditional (ternary) operator usage

Benjamin Vincent I'm getting a cryptic error message when using two custom Typecsipt types (temporary name sorry) and the ternary operator, no error message when I use only 'OptionsPostData3 | OptionsPostData4'or only OptionsPostData3half or OptionsPostData4th

Operand type is incompatible with operator

Noppadet i get error Operand type is incompatible with operator Click OK in the header when trying to compare two "real" data types. Can anyone help me with what problem? public void clicked() { real localAnnualUsage = itemSetup_DS.AnnualUsage();

Operand type is incompatible with operator

Noppadet i get error Operand type is incompatible with operator Click OK in the header when trying to compare two "real" data types. Can anyone help me with what problem? public void clicked() { real localAnnualUsage = itemSetup_DS.AnnualUsage();

operands incompatible with ternary operator

dxdt While writing lambda expressions, I stumbled upon a strange problem. Here is the code: #if 1 /* exhibit A */ const auto giveup = this->__randomGiveUp(); auto evtFunc = [this, giveup]() { this->__acquireLock(); if (giveu

operands incompatible with ternary operator

dxdt While writing lambda expressions, I stumbled upon a strange problem. Here is the code: #if 1 /* exhibit A */ const auto giveup = this->__randomGiveUp(); auto evtFunc = [this, giveup]() { this->__acquireLock(); if (giveu

operands incompatible with ternary operator

dxdt While writing lambda expressions, I stumbled upon a strange problem. Here is the code: #if 1 /* exhibit A */ const auto giveup = this->__randomGiveUp(); auto evtFunc = [this, giveup]() { this->__acquireLock(); if (giveu