“探索 C”书”编译器设置
我刚刚拿到《探索 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
not
是布尔运算符!
的“替代标记”。也许你的编译器不支持它。
试试这个:
确实,这是另一个网站上完全相同的相同问题。
事实上,Visual Studio 要求您
#include
才能获得对替代令牌的支持,尽管 C++ 标准声明这不会产生任何效果1。顽皮的视觉工作室!我推荐这些资源。
not
is an "alternative token" for the boolean operator!
.Perhaps your compiler doesn't support it.
Try this instead:
Indeed, here's exactly the same issue on another site.
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!I recommend these resources.
Try
而不是
,因为这是大多数 C++ 程序员习惯的代码风格。
Try
instead of
as this is the code style that most C++ programmers are used to.
您不应该使用
/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.