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

时间:2011-09-11 18:29:51

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

我刚拿到这本书“探索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"));
}

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

如果这对我来说非常苛刻,我会道歉。

3 个答案:

答案 0 :(得分:5)

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

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

请改为尝试:

if (!in)

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

  

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

事实上,Visual Studio要求您#include <ciso646>获得替代令牌的支持,即使C ++标准声明这应该没有效果 1 。 Naughty Visual Studio!

  

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

我建议these resources


  

1 [n3290: footnote 176]:特别是,包括标准标题<iso646.h><ciso646>无效。

答案 1 :(得分:0)

尝试

if (!in)

而不是

if (not in)

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

答案 2 :(得分:0)

您不应该使用/za。问题是它在打开时会导致许多编译器错误,而且SFINAE等更重要的编译器问题无法解决,而且像Windows头文件这样的标题将无法编译。

从技术上讲,not关键字用于!运算符。您可能会发现MSVC不支持它,因此请直接使用!