在GHCi中:
- 序曲>(+3)2
5- 序曲>(*3)2
6- 前奏>(/ 3)2
0.6666666666666666- 前奏>( - 3)2
没有实例(Num(t - > t1))
来自文字':it =( - 3)23' at <interactive>:1:2
Possible fix: add an instance declaration for (Num (t -> t1))
In the expression: 3
In the expression: (- 3) 2
In the definition of
如何更正最后一个使其返回-1?
有人可以告诉我为什么C编译器在使用a Compound Assignment和a Prefix Dec/Inc时输出错误?[但C++不]
int myVar = 5;
(--myVar) -= 4;
// C : error C2106: '-=' : left operand must be l-value
// C++: myVar=0;
Run Code Online (Sandbox Code Playgroud)
我知道错误说的是什么......
但是,我无法理解为什么C编译器无法识别myVar为l值而是C++?
当使用运算符重载前缀和后缀增量时,我从编译器收到错误:
"Fajl Fajl :: operator ++(int)':已定义或声明的成员函数"
以下是operator ++的标题:
Fajl& operator ++ (); // prefix
Fajl& operator -- (); // prefix
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
Run Code Online (Sandbox Code Playgroud)
而我的实施:
Fajl& Fajl::operator ++ () // prefix
{
++(*poz);
return *this;
}
Fajl& Fajl::operator -- () // prefix
{
--(*poz);
return *this;
}
Fajl Fajl::operator ++ (int dummy) // postfix
{
Fajl temp(*this);
++(*this);
return temp;
}
Fajl Fajl::operator -- (int dummy) // postfix
{
Fajl temp(*this);
--(*this); …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading prefix-operator postfix-operator operator-keyword
为什么下面的代码是非法的?
using System;
class Program
{
static void Main(string[] args) {
int i = 0;
--i++;
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误--i++:
The operand of an increment or decrement operator must be a variable, property or indexer
我知道这段代码没有实际用途; 我只是好奇为什么不允许这样做.我不关心它可以通过删除该行而没有其他影响来修复.由于这是用语言律师标记的,请包括语言规范中的证据.
以下表达式:-
int main()
{
int x=2, y=9;
cout << ( 1 ? ++x, ++y : --x, --y);
}
Run Code Online (Sandbox Code Playgroud)
给出以下输出:-
9
Run Code Online (Sandbox Code Playgroud)
根据我的理解,它应该返回 ++y,它应该是 10。出了什么问题?
c++ conditional-operator operator-precedence prefix-operator
我听到一位教授说“避免在上下文允许选择前缀的后缀运算符”。我进行搜索,但在stackoverflow中找不到相关的帖子来解释这一点。
当我们有能力选择操作员++时,为什么更喜欢使用prefixoperator ++ postfix?
taking_damage在此代码示例中,方法之前的感叹号的目的是什么?
if warrior.health < 20 && !taking_damage?(warrior)
warrior.rest!
end
Run Code Online (Sandbox Code Playgroud) 我对C中的postfix和前缀运算符优先级感到困惑,任何帮助和提示都会有所帮助.
我会在这里粘贴我的测试代码:
#include <stdio.h>
int a = 0;
int main(int argc, char *argv[])
{
if (++a & 0x01) // prefix version
// if (a++ & 0x01) // postfix version
{
printf("++ first\n");
}
else
{
printf("& first\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我现在可以理解,在postfix版本中,虽然postfix ++有一个更大的优先级,但是a++会返回0到这里&,0x01并且会a在此表达式后增加值.
但我无法理解的是为什么在前缀版本中,为什么++a首先进行评估?运算符优先级表指示prefix ++并&具有相同的优先级,此外,它们的关联性为right-to-left.这种方法&不应该先评估吗?
编辑: 我正在使用的图表:C运算符优先表
我是 Haskell 的新手,不知道哪个是惯用的 - 使用前缀形式或中缀形式的运算符。从我目前为止发现,有特殊字符功能名称,如<$>,<*>,>>=等在中缀形式,但功能,其名称由字母最多的前缀形式使用使用。我的猜测是这是从数学中获得的灵感,尽管我不确定。
我查过Blow your mind , Category:Idioms并搜索了 stackoverflow ,但无济于事。
澄清一下,什么时候中缀函数和前缀运算符是惯用的?
我面临着一个关于postfix和前缀运算符的经典考试问题,前缀是我无法理解的.考虑以下:
#define MAX( a, b ) ( a > b ) ? (a) : (b)
int main()
{
int x = 2, y = 2;
if( MAX( ++x, y ) == x )
{
printf( " %d is greater than %d ", x, y );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
考试问题要求提供该课程的输出.对我来说,它将是"3大于2"但实际输出是"4大于2"
我理解post和前缀是如何工作的(或者至少我是这么认为的)但是我不知道变量如何递增两次.对此有何解释?