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 cont开发者_如何学编程inue with this book so any help is greatly appreciated.
And I apologize if this is awfully noobish of me.
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.
Try
if (!in)
instead of
if (not in)
as this is the code style that most C++ programmers are used to.
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.
精彩评论