相关疑难解决方法(0)

我可以在一行上写一个带有变量声明的if语句吗?

我想知道是否有办法把它放在一条线上?

if (auto r = getGlobalObjectByName(word)) r->doSomething; // This works fine

if (!auto r = getGlobalObjectByName(word)) r->doSomething; // Says "expected an expression"

if (auto r = getGlobalObjectByName(word) == false) r->doSomething; // Also doesn't work.
Run Code Online (Sandbox Code Playgroud)

我也尝试用额外的括号围绕它,但这似乎不起作用.我发现这在一条线上非常方便.

c++ if-statement

26
推荐指数
2
解决办法
4496
查看次数

'...' 之前预期的主表达式,C++ 编译错误

SO 上有很多类似标题的帖子,但它们似乎是由各种语法错误触发的,而且我还没有看到一致的模式。

\n
using namespace std;\n\nclass A\n{\npublic:\n    A(int a_) : a(a_) {}\n    int a;\n};\n\nint main()\n{\n    A x{3};\n    A y{0};\n\n    if ((y=x).a)\n        cout << y.a << endl;\n        \n    int i = 1;\n    if (int j = i)\n        cout << j << endl;\n    \n    if ((A z = x).a) // error: expected primary-expression before \xe2\x80\x98z\xe2\x80\x99\n        cout << z.a << endl;\n\n    (int m = 1); // error: expected primary-expression before \xe2\x80\x98int\xe2\x80\x99\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我是否错误地假设A z = x是一个赋值表达式,它应该与 具有相同的值z

\n

c++ syntax compiler-errors

0
推荐指数
1
解决办法
2873
查看次数

标签 统计

c++ ×2

compiler-errors ×1

if-statement ×1

syntax ×1