【问题标题】:Why does CLion not allow me to have nested ternary operators?为什么 CLion 不允许我使用嵌套的三元运算符?
【发布时间】:2018-07-22 02:11:35
【问题描述】:

我正在尝试在 C++14 中嵌套三元表达式。我的代码如下:

#include <bits/stdc++.h>

using namespace std;

int main() {
    int i = 5;
    string result = i % 2 == 0 ? "a" : i % 3 == 0 ? "b" : "c";

    return 0;
}

我正在使用 CLion。我在最后一个下一个表达式 i % 3 == 0 ? "b" : "c"Type 'const char[2]' and 'const char[2]' are not compatible 时遇到错误。我做错了什么?

如果有帮助,这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(TestCLion)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES relop.cpp)
add_executable(TestCLion ${SOURCE_FILES})

【问题讨论】:

  • FWIW 在 MacOS/X 上的 clang/LLVM 9.0.0 下为我编译的已发布代码没有错误(在将 #include 行更改为 #include 之后)。所以你可能会在你的特定编译器中看到一个错误
  • 请做一个最小的例子。
  • @KerrekSB 更新
  • 如果这是 IDE,而不是编译器,那么给它一点提示可能会有所帮助,这一切都是关于指针的。例如。将"a" 替换为&amp;*"a",或者只引入一个虚拟char const*。不能保证,但值得一试。
  • 你不应该包括&lt;bits/stdc++.h&gt;

标签: c++ nested c++14 ternary-operator clion


【解决方案1】:

这是known bug in CLion

代码将无错误地编译。 (使用 CLion 2017.3.1、GCC 6.3.0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-30
    • 2011-10-29
    • 2014-10-09
    • 2020-07-06
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多