编译器说逻辑和未定义的标识符

时间:2017-11-25 19:47:53

标签: c++ cl.exe

我在mac上学习了c ++,最近转移到了Windows 7.我下载了windows v7.1 sdk并运行了安装程序。它是sdk的.net 4依赖版本,我安装了.net 4。

我正在使用命令行,因为我更喜欢使用它,我使用mac上的gcc编译器做到了这一点,并且我很擅长它,因为我对编程很新。

我一直在使用v7.1 sdk开发人员命令提示符,因为它使用SetEnv批处理文件设置环境变量。

编译器显然是Microsoft的cl.exe编译器。

我运行了典型且非常简单的hello world程序,包括最后的getchar()让我实际看到程序,因为mac并不需要它。并且getchar运行良好,程序编译并运行良好。

当我尝试编译我在mac上编写的一些源代码时出现问题。顺便说一句,在mac上编译好了。它开始抛出一些非常奇怪的错误,比如告诉我逻辑错误'和' operator是未定义的标识符。现在我可能是一个愚蠢的人,但从我的理解,运营商不是一个标识符,它是一个运营商。

所以我决定通过编写一个非常简单的程序来缩小问题范围,该程序使用一个if语句和一个else语句以及'和'操作员,看看会发生什么。下面是我试图编译的代码:

//hello, this is a test

#include <iostream>

int main()

{

    char end;
    int a = 0, b = 0;

    std::cout << "If the variable a is larger than 10 and variable b is less than a, then b will be subtracted from a, else they are added.\n";
    std::cout << "Enter a number for variable a\n";
    std::cin >> a;
    std::cout << "Now enter a number for variable b\n";
    std::cin >> b;

    if (a>10 and b<a) a - b;
    else a+b;
    std::cout << "The value of a is: " <<a;

    std::cout << "Press any key to exit";
    end = getchar();
    return 0;
}

这是我用来编译程序的命令

cl /EHsc main.cpp

最后但并非最不重要的是,这个程序提出的错误列表,为什么这些错误在这里我不确定。它对我没有任何意义。

的main.cpp

error C2146: syntax error : missing ')' before identifier 'and'

error C2065: 'and' : undeclared identifier

error C2146: syntax error : missing ';' before identifier 'b'

error C2059: syntax error : ')'

error C2146: syntax error : missing ';' before identifier 'a'

warning C4552: '<' : operator has no effect; expected operator with side-effect

warning C4552: '-' : operator has no effect; expected operator with side-effect

error C2181: illegal else without matching if

warning C4552: '+' : operator has no effect; expected operator with side-effect

这些错误中的每一个都是奇怪的。我以前从未见过它,之前我从未问过一个问题,因为我总是能够在不问的情况下找到答案,但在这一点上我真的很难过。

2 个答案:

答案 0 :(得分:5)

这是Microsoft Visual C ++编译器中的一个错误(一项功能) - 它不支持关键字AntialiasedLineEnableandand_eqbitand,{{1} },bitorcomplnotnot_eqoror_eq。您应该使用更常用的运算符,例如xor而不是xor_eq&&而不是and等等价表:

||

与C ++不同,C不提供这些关键字,而是提供带有一组宏的标头or,这些宏的名称可扩展为这些逻辑运算符。这样做是为了支持过去在键盘上没有所需字符的机器。

因为C ++试图尽可能地避免使用宏,所以C ++标头等效+--------+-------+ | and | && | | and_eq | &= | | bitand | & | | bitor | | | | compl | ~ | | not | ! | | not_eq | != | | or | || | | or_eq | |= | | xor | ^ | | xor_eq | ^= | +--------+-------+ 没有定义任何宏,而是以内置关键字的形式提供。

正如此处所述,较新版本的MSVC可能会为此添加一些支持,但您应该知道那些&#34; altenative运算符&#34;很少使用。我建议你坚持使用原始的C语法。

答案 1 :(得分:1)

这些alternative operators<iso646.h>标头中定义为Visual C ++实现中的宏。它们与其他人一起构建在C ++语言中。包含此标题:

#include <iso646.h>

使用Visual C ++编译器时使用/permisive-编译器开关进行编译。 Permissive compiler switch 在使用GCC进行编译时,您不需要包含上述标题。 GCC实现似乎尊重alternative operators representation引用,其中指出:

  

相同的单词在包含文件<iso646.h>中的C编程语言中定义为宏。因为在C ++中这些内置于语言中,所以<iso646.h>的C ++版本以及<ciso646>都没有定义任何内容。

Live example on Coliru