【问题标题】:"Exploring C++ book" compiler setup《探索 C++ 书》编译器设置
【发布时间】:2011-09-11 18:29:51
【问题描述】:

我刚拿到这本书“探索 C++”,我正在上第一课。作为一种爱好,我从事 C# 已经有几年了,所以我为什么不试试 C++。

书中说我需要设置我的编译器以使用标准 C++。我正在使用visual studio 2010,所以我做到了。 http://msdn.microsoft.com/en-us/library/ms235629.aspx

但是当我去编译代码时,除了一个 if 语句之外,它都可以正常工作。

我按照说明进行了三次检查,所以它一定是工具的问题。

特别是

if (not in) // this line here
{
    std::perror(argv[1]);
    return EXIT_FAILURE;

}

完整样本

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <ostream>
#include <string>
#include <vector>

void read(std::istream& in, std::vector<std::string>& text)
{
    std::string line;
    while (std::getline(in, line))
        text.push_back(line);
}

int main(int argc, char* argv[])
{
    std::vector<std::string> text;

    if (argc <2)
        read(std::cin, text);
    else 
    {
        std::ifstream in(argv[1]);
        if (not in)
        {
            std::perror(argv[1]);
            return EXIT_FAILURE;

        }
        read(in,text);
    }

    std::sort(text.begin(), text.end());

    std::copy(text.begin(), text.end(),
        std::ostream_iterator<std::string>(std::cout, "\n"));
}

我真的很想继续阅读这本书,因此非常感谢任何帮助。

如果这对我来说非常愚蠢,我深表歉意。

【问题讨论】:

标签: c++ visual-studio visual-studio-2010 standard-library


【解决方案1】:

not 是布尔运算符! 的“替代标记”。

也许你的编译器不支持它。

试试这个:

if (!in)

确实,here's exactly the same issue on another site

VC 编译器默认不识别替代标记(它们现在非常罕见),但我相信可以通过编译器开关打开此支持。

事实上,Visual Studio 要求您 #include &lt;ciso646&gt; 获得对替代令牌的支持,即使 C++ 标准声明这应该无效1。调皮的 Visual Studio!

无论如何,您可能希望找到一本更好、更现代的教科书。

我推荐these resources


1[n3290: footnote 176]: 特别是包含标准标头&lt;iso646.h&gt;&lt;ciso646&gt; 无效。

【讨论】:

  • 谢谢,这解决了我的问题。现在我有两个不指向任何代码的错误。 2008 年出版的一本书没有提到这一点似乎很奇怪。无论如何,我宁愿有更多最新的东西,所以我要检查你链接的那些资源。再次感谢这些。
  • 在 Visual Studio 中,您还可以使用 /Za 选项禁用 Microsoft 扩展。有趣的是,他们的扩展之一是禁用替代令牌。
  • @Seth:它已被弃用。我确定你的意思是&lt;ciso646&gt;
  • Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup d:\CPPTut\Tutorial\Tutorial\MSVCRTD.lib(crtexew.obj) Tutorial
  • Error 2 error LNK1120: 1 unresolved externals d:\CPPTut\Tutorial\Debug\Tutorial.exe 1 1 教程
【解决方案2】:

试试

if (!in)

而不是

if (not in)

因为这是大多数 C++ 程序员习惯的代码风格。

【讨论】:

    【解决方案3】:

    您不应该使用/za。问题是它在打开时会导致许多编译器错误,并且更重要的编译器问题(如 SFINAE)无论如何都无法解决,并且某些头文件(如 Windows 头文件)无法编译。

    从技术上讲,not 关键字用于! 运算符。你可能会发现 MSVC 不支持,所以直接使用!即可。

    【讨论】:

    • 好的,是的。 ([n3290: C.3.2.3/1])。 2.12 对此有点模棱两可,但我们开始了。
    • 其实还不错。我们将特定于 Windows 的代码和可移植代码分开,并且可以将 /Za 用于可移植部分。 windows.h 不能与 /Za 一起编译是我们的一个特性;它可以防止事故。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2015-06-21
    • 2011-07-04
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2020-08-17
    相关资源
    最近更新 更多