I figured that Clang (version 3.0 trunk 127530) must work, since it is so highly praised for its standards conformance. Unfortunately, it didn't even give me one of its pretty, highlighted error messages:

To be honest, I don't really know what any of these error message mean.

Many other C++ programs have source files with a .cpp extension, so I thought perhaps I needed to rename my file. I changed its name to helloworld.cpp, but that didn't help. I think there is a very serious bug in Clang because when I tried using it to compile the renamed program, it flipped out, printed "84 warnings and 20 errors generated." and made my computer beep a lot!

What have I done wrong here? Have I missed some critical part of the C++ Standard? Or are all three compilers really just so broken that they can't compile this simple program?

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

31 Answers
31

Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) if necessary.

Your compiler doesn't support that format (aka cannot map it to the basic source character set), so it cannot move into further processing stages, hence the error. It is entirely possible that your compiler support a mapping from image to basic source character set, but is not required to.

Since this mapping is implementation-defined, you'll need to look at your implementations documentation to see the file formats it supports. Typically, every major compiler vendor supports (canonically defined) text files: any file produced by a text editor, typically a series of characters.

Note that the C++ standard is based off the C standard (§1.1/2), and the C(99) standard says, in §1.2:

This International Standard does not specify
— the mechanism by which C programs are transformed for use by a data-processing
system;
— the mechanism by which C programs are invoked for use by a data-processing
system;
— the mechanism by which input data are transformed for use by a C program;

So, again, the treatment of source files is something you need to find in your compilers documentation.

I think that sentence is ambiguous at best. The Merriam-Webster dictionary says that text is the original words and form of a written or printed work or a work containing such text. This source file clearly falls under that definition. Do you think I should file a defect report with the Core Language Working Group?
–
James McNellisApr 1 '11 at 1:27

While I don't appreciate you making fun of my handwriting, this might be the real issue, and would explain the error I get when I try compiling the renamed helloworld.cpp with Visual C++: "fatal error C1004: unexpected end-of-file found" I'll try again and report back soon. Thanks!
–
James McNellisApr 1 '11 at 1:31

37

@James make sure you turn off all png optimizations. it makes debugging easier.
–
wilhelmtellApr 1 '11 at 2:30

I've found it helps to not write my code on my monitor's glass with a magic marker, even though it looks nice when its really black. The screen fills up too fast and then the people who give me a clean monitor call me names each week.

A couple of my employees (I'm a manager) are chipping in to buy me one of those red pad computers with the knobs. They said that I won't need markers and I can clean the screen myself when it's full but I have to be careful shaking it. I supposed it's delicate that way.

File format not recognized You need to properly format your file. That means using the right colors and fonts for your code. See the specific documentations for each compiler as these colors vary between compiler ;)

Did you handwrite the program and then scan it into the computer? That is what is implied by "helloworld.png". If that is the case, you need to be aware that the C++ standard (even in its newest edition) does not require the presence of optical character recognition, and unfortunately it is not included as an optional feature in any current compiler.

You may want to consider transposing the graphics to a textual format. Any plain-text editor may be used; the use of a word processor, while capable of generating a pretty printout, will most likely result in the same error that you get while trying to scan.

If you are truly adventurous, you may attempt to write your code into a word processor. Print it, preferably using a font like OCR-A. Then, take your printout and scan it back in. The scan can then be run through a third-party OCR package to generate a text form. The text form may then be compiled using one of many standard compilers.

Beware, however, of the great cost of paper that this will incur during the debugging phase.

Unfortunately, you have selected three compilers that all support multiple languages, not just C++. They all have to guess at the programming language you used. As you probably already know, the PNG format is suitable for all programming languages, not just C++.

Usually the compiler can figure out the language itself. For instance, if the PNG is obviously drawn with crayons, the compiler will know it contains Visual Basic. If it looks like it's drawn with a mechanical pencil, it's easy to recognize the engineer at work, writing FORTRAN code.

This second step doesn't help the compiler either, in this case. C and C++ just look too similar, down to the #include. Therefore, you must help the compiler decide what language it really is. Now, you could use non-standard means. For instance, the Visual Studio compiler accepts the /TC and /TP command-line arguments, or you could use the "Compile as: C++" option in the project file. GCC and CLang have their own mechanisms, which I don't know.

Therefore, I'd recommend using the standard method instead to tell your compiler that the code following is in C++. As you've discovered by now, C++ compilers are very picky about what they accept. Therefore the standard way to identify C++ is by the intimidation programmers add to their C++ code. For instance, the following line will clarify to your compiler that what follows is C++ (and he'd better compile it without complaints).

// To the compiler: I know where you are installed. No funny games, capice?

The first problem is, that you are trying to return an incorrect value at the end of the main function. C++ standard dictates that the return type of main() is int, but instead you are trying to return the empty set.

The other problem is - at least with g++ - that the compiler deduces the language used from the file suffix. From g++(1):

For any given input file, the file
name suffix determines what kind of
compilation is done:

file.cc file.cp file.cxx file.cpp file.CPP file.c++ file.C

C ++ source code which must be preprocessed. Note that in .cxx, the
last two letters must both be literally x. Likewise, .C refers to a
literal capital C.

Fixing these should leave you with a fully working Hello World application, as can be seen from the demo here.

I had a professor way back when who would take off points your homework or exams if you put a slash through a zero digit since zero isn't the null set. He would appreciate this answer.
–
Michael BurrApr 1 '11 at 20:44

Haha, but I finally found an easy one I could answer!
–
Cody GrayApr 1 '11 at 12:32

10

This is silly. We all know the compiler would optimize out the whitespace, leaving only heavily compressed black-space, which is all ones and would compress down to a binary 1 which would be returned as an error. The code needed to be written using white-out which would compile to 0 and not return an error.
–
the Tin ManApr 1 '11 at 15:04

Try switching input interface. C++ expects a keyboard to be plugged in to your computer, not a scanner. There may be peripherals conflict issues here. I didn't check in ISO Standard if keyboard input interface is mandatory, but that is true for all compilers I ever used. But maybe scanner input is now available in C99, and in this case your program should indeed work. If not you'll have to wait the next standard release and upgrade of compilers.

It's a capital letter O with a special line we call "diameter", which tells the compiler to use the Midpoint Circle Algorithm, obviously. I think you should get your eyes checked.
–
Mateen UlhaqApr 9 '11 at 5:03