gpt4 book ai didi

c++ - "Exploring C++ book"编译器设置

转载 作者:太空狗 更新时间:2023-10-29 20:46:45 26 4
gpt4 key购买 nike

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

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

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

最佳答案

not是 bool 运算符 ! 的“替代标记” .

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

试试这个:

if (!in)

确实,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.

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

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

我推荐these resources .


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

关于c++ - "Exploring C++ book"编译器设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7380376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com