“探索 C”书”编译器设置

发布于 2024-12-03 19:03:23 字数 1387 浏览 3 评论 0原文

我刚刚拿到《探索 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"));
}

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

如果这对我来说太菜鸟了,我深表歉意。

I just got this book "Exploring C++" and I'm on my first lesson. I've been doing C# for a couple years as a hobby so i though why not give C++ a try.

In the book it says i need to setup my compiler to use standard C++. I am using visual studio 2010 so i did. http://msdn.microsoft.com/en-us/library/ms235629.aspx

but when i go to compile the code it all works fine except for one if statement.

i have triple checked just as instructed so it must be something with the tools.

specifically

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

}

The full sample

#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"));
}

I would really like to continue with this book so any help is greatly appreciated.

And I apologize if this is awfully noobish of me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拿命拼未来 2024-12-10 19:03:23

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

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

试试这个:

if (!in)

确实,这是另一个网站上完全相同的相同问题

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

事实上,Visual Studio 要求您#include 才能获得对替代令牌的支持,尽管 C++ 标准声明这不会产生任何效果1。顽皮的视觉工作室!

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

我推荐这些资源


1 [n3290: footnote 176]: 特别是,包括标准标头 < ;ciso646> 无效。

not is an "alternative token" for the boolean operator !.

Perhaps your compiler doesn't support it.

Try this instead:

if (!in)

Indeed, here's exactly the same issue on another site.

VC compiler doesn't by default recognize alternative tokens (they are exceedingly rare nowadays), but I believe this support may be turned on with a compiler switch.

In fact, Visual Studio requires that you #include <ciso646> to get support for alternative tokens, even though the C++ Standard states that this should have no effect1. Naughty Visual Studio!

In any case, you might want to find a better, more modern textbook.

I recommend these resources.


1 [n3290: footnote 176]: In particular, including the standard header <iso646.h> or <ciso646> has no effect.

紙鸢 2024-12-10 19:03:23

Try

if (!in)

而不是

if (not in)

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

Try

if (!in)

instead of

if (not in)

as this is the code style that most C++ programmers are used to.

花桑 2024-12-10 19:03:23

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

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

You shouldn't use /za. The thing is that it causes numerous compiler bugs when switched on and more important compiler problems like SFINAE aren't resolved anyway, and some headers like Windows headers won't compile.

Technically, the not keyword is used for the ! operator. You may find that MSVC doesn't support it, so just use ! directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文