3

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.

leetbit
  • 133
  • 1
  • 10
  • according to this post http://stackoverflow.com/questions/555505/c-alternative-tokens you should use /Za switch to disable the extension. – Jichao Apr 14 '12 at 13:41

3 Answers3

5

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thank you, that solved my not problem. Now i have two errors that do not point to any code. Seems strange that a book published in 2008 wouldn't mention that. Anyway, id rather have something more up to date so Im going to check out those resources you linked. Thanks again for those. – leetbit Sep 11 '11 at 18:37
  • In Visual Studio you can also use the /Za option to disable Microsoft extensions. Interestingly, one of their _extensions_ is to disable alternative tokens. – D Krueger Sep 11 '11 at 18:38
  • @Seth: And it's deprecated. I'm sure you meant ``. – Lightness Races in Orbit Sep 11 '11 at 18:40
  • Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup d:\CPPTut\Tutorial\Tutorial\MSVCRTD.lib(crtexew.obj) Tutorial – leetbit Sep 11 '11 at 18:42
  • Error 2 error LNK1120: 1 unresolved externals d:\CPPTut\Tutorial\Debug\Tutorial.exe 1 1 Tutorial – leetbit Sep 11 '11 at 18:43
  • @leetbit: Iterative debugging discussion is not a good fit for this site's Q&A format. Perhaps you'd be happier in [one of the chatrooms](http://chat.stackoverflow.com/)? – Lightness Races in Orbit Sep 11 '11 at 18:44
  • @Tomalak Geret'kal: ah.. no rep. I am gonna use one of your resources now. Thanks for the help :) – leetbit Sep 11 '11 at 18:45
  • @Tom no, I actually did mean `` because it does work, but like you said (and like I just found out), it's deprecated also, so to everyone who reads this, use `ciso646`. – Seth Carnegie Sep 11 '11 at 18:50
0

Try

if (!in)

instead of

if (not in)

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

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
0

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • OK, it is. (`[n3290: C.3.2.3/1]`). `2.12` is a little more ambiguous about it, but there we go. – Lightness Races in Orbit Sep 11 '11 at 18:43
  • It's actually not so bad. We separate Windows-specific and portable code, and can use `/Za` for the portable parts. That `windows.h` won't compile with `/Za` is a feature for us; it prevents accidents. – MSalters Sep 12 '11 at 08:51