467

I am having some difficulty compiling a C++ program that I've written.

This program is very simple and, to the best of my knowledge, conforms to all the rules set forth in the C++ Standard. I've read over the entirety of ISO/IEC 14882:2003 twice to be sure.

The program is as follows:

enter image description here

Here is the output I received when trying to compile this program with Visual C++ 2010:

c:\dev>cl /nologo helloworld.png
cl : Command line warning D9024 : unrecognized source file type 'helloworld.png', object file assumed
helloworld.png : fatal error LNK1107: invalid or corrupt file: cannot read at 0x5172

Dismayed, I tried g++ 4.5.2, but it was equally unhelpful:

c:\dev>g++ helloworld.png
helloworld.png: file not recognized: File format not recognized
collect2: ld returned 1 exit status

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:

c:\dev>clang++ helloworld.png
helloworld.png: file not recognized: File format not recognized
collect2: ld returned 1 exit status
clang++: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

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?

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
James McNellis
  • 348,265
  • 75
  • 913
  • 977

31 Answers31

575

Originally from Overv @ reddit.

Sven
  • 2,839
  • 7
  • 33
  • 53
320

Try this way:

enter image description here

Benoit
  • 76,634
  • 23
  • 210
  • 236
211

Your < and >, ( and ), { and } don't seem to match very well; Try drawing them better.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 44
    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 McNellis Apr 01 '11 at 01:31
  • 37
    @James make sure you turn off all png optimizations. it makes debugging easier. – wilhelmtell Apr 01 '11 at 02:30
  • 5
    @James: "unexpected end-of-file" almost certainly means that it is your `}` that is causing the problem. Try focusing on matching that with the `{` – Carson63000 Apr 01 '11 at 22:10
173

In the standard, §2.1/1 specifies:

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 23
    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 McNellis Apr 01 '11 at 01:27
  • @James: I have corrected my answer. – GManNickG Apr 01 '11 at 01:45
  • 15
    Oh; I completely forgot to read all of the referenced documents. I think that paragraph is taken out of context, though, so I shall go and read the entirety of ISO/IEC 9899:1990 and will post back here once I fully understand it. – James McNellis Apr 01 '11 at 02:22
156

You could try the following python script. Note that you need to install PIL and pytesser.

from pytesser import *
image = Image.open('helloworld.png')  # Open image object using PIL
print image_to_string(image)     # Run tesseract.exe on image

To use it, do:

python script.py > helloworld.cpp; g++ helloworld.cpp
sje397
  • 41,293
  • 8
  • 87
  • 103
110

You forgot to use Comic Sans as a font, that's why its erroring.

bl00dshooter
  • 991
  • 2
  • 10
  • 17
  • 73
    Unfortunately, this is the only font that my hand supports. That would be very sad if I am unable to program in C++ because of this. Do you think Java would support this font? – James McNellis Apr 01 '11 at 01:35
  • 8
    You will need Comic Sans when you think of drawing comics anyway, so you should seriously consider upgrading hands. – sharptooth Apr 01 '11 at 05:44
  • 8
    C++ requires year-long training in calligraphy. If you don't have the time, try Visual Basic or just binary machine code (You just have to get the 0's and 1's right then). – Frank Osterfeld Apr 01 '11 at 11:46
  • 1
    @Frank C++0x §42.1/1 specifies "All strings must be in Gothic." – Mateen Ulhaq Apr 09 '11 at 04:17
75

I can't see a new-line after that last brace.

As you know: "If a source file that is not empty does not end in a new-line character, ... the behavior is undefined".

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 16
    Hmmm. Thankfully this ridiculous rule has been removed in C++0x. That said, how does one end such a file with a newline? I thought I had left sufficient room at the end of the text (if you highlight the source file, you should see the extra room I left). Thanks for the tip, though! – James McNellis Apr 01 '11 at 01:25
  • 8
    If you don't have enough whitespace I can try compiling it on my system. I have four monitors so I could try compiling from my leftmost one. – the Tin Man Apr 01 '11 at 15:12
74

This program is valid -- I can find no errors.

My guess is you have a virus on your machine. It would be best if you reformat your drive, and reinstall the operating system.

Let us know how that works out, or if you need help with the reinstall.

I hate viruses.

Jerry Asher
  • 836
  • 2
  • 10
  • 23
62

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.

That's why I hire the smart people.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 2
    A Wacom Cintiq is much more appropriate for a manager. It's expensive, and makes you feel really important. Any graphic designers in your company will have much lower status, and should therefore use EGA monitors. Janitors should use CGA monitors. Programmers should use second hand monochrome terminals. –  Apr 01 '11 at 12:43
  • 7
    I had a "Life Like" monitor for a long time. It was so realistic that you'd swear the screensaver of swimming fish was real, and the little diver man looked like he was swimming. I kept getting my arm wet trying to get the treasure chest from the bottom it was so real. The only problem was the screen saver was always on and the realistic bubbling noises made it hard to hear. Oh, and they said for maintenance I had to sprinkle stuff in the top of the monitor daily or the screen saver would stop working. It did that once, and boy, the smell two days later was really realistic. – the Tin Man Apr 01 '11 at 14:58
59

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 ;)

helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • 14
    Oh, that kind of makes sense... I have a box of 96 crayons, so I'm sure I have the correct foreground color. I'll pick up some colored construction paper tomorrow and try it out on a different color of paper. – James McNellis Apr 01 '11 at 01:34
  • 3
    Just to be safe, you better get some coloring pencils and oil-based paint as well. It's a well known fact that C++ is meant to be a very difficult language to format correctly. – helloworld922 Apr 01 '11 at 04:22
  • Yeap, and don't forget to use the highlighting marker. – sharptooth Apr 01 '11 at 05:41
  • 6
    @sharptooth - syntax highlighting is an IDE feature - you're not meant to do it by hand. So make sure you get a robot arm to go with that highlighting marker. –  Apr 01 '11 at 12:35
56

You forgot the pre-processor. Try this:

pngtopnm helloworld.png | ocrad | g++ -x 'c++' -
dizel3d
  • 3,619
  • 1
  • 22
  • 35
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • 8
    Oh! I thought the preprocessor was included with the compiler! I'll try to find a preprocessor that works on my Windows laptop. – James McNellis Apr 01 '11 at 01:37
  • 3
    @James McNellis: The preprocessor isn't a program, it's a hardware thing that looks like a highlighting marker - you move it over your text and it gets preprocessed. – sharptooth Apr 01 '11 at 14:05
49

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.

Kevin Lacquement
  • 5,057
  • 3
  • 25
  • 30
46

Draw the include below to make it compile:

#include <ChuckNorris>

I hear he can compile syntax errors...

Grofit
  • 17,693
  • 24
  • 96
  • 176
40

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?
MSalters
  • 173,980
  • 10
  • 155
  • 350
33

Try this one:

Do you see the dinosaur in the space shuttle?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
  • 4
    I think there's a typo - it should be `endl` (L) not `end1` (one). But +1 nicely done! – Rup Apr 01 '11 at 16:03
  • 44
    I've been staring at this for three hours but I still can't see a dinosaur or the space shuttle. :-( – oosterwal Apr 02 '11 at 03:33
32

Is your compiler set in expert mode?! If yes, it shouldn't compile. Modern compilers are tired of "Hello World!"

zamanbakshi
  • 266
  • 2
  • 6
27

OCR Says:

N lml_�e <loJ+_e__}

.lnt Mk.,n ( ln+ _rSC Lhc_yh )
h_S_
_l

s_l . co__ <, " H llo uo/_d ! '` << s l� . ena_ .
TP__rn _ |
_|

Which is pretty damn good, to be fair.

Robin Duckett
  • 2,094
  • 3
  • 16
  • 15
26

helloworld.png: file not recognized: File format not recognized

Obviously, you should format your hard drive.

Really, these errors aren't that hard to read.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
20

I did convert your program from PNG to ASCII, but it does not compile yet. For your information, I did try with line width 100 and 250 characters but both yield in comparable results.

   `         `  .     `.      `         ...                                                         
   +:: ..-.. --.:`:. `-` .....:`../--`.. `-                                                         
           `      `       ````                                                                      
                                                                      `                             
   ` `` .`       ``    .`    `.               `` .      -``-          ..                            
   .`--`:`   :::.-``-. : ``.-`-  `-.-`:.-`    :-`/.-..` `    `-..`...- :                            
   .`         ` `    ` .`         ````:``  -                  ` ``-.`  `                            
   `-                                ..                           ``                                
    .       ` .`.           `   `    `. ` .  . `    .  `    . . .` .`  `      ` ``        ` `       
           `:`.`:` ` -..-`.`-  .-`-.    /.-/.-`.-.  -...-..`- :```   `-`-`  :`..`-` ` :`.`:`- `     
            ``  `       ```.      ``    ````    `       `     `        `    `         `   `   .     
            : -...`.- .` .:/ `                                                                      
    -       `             `` .                                                                      
    -`                                                                                              
    `                                                                                               
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Yves
  • 61
  • 2
  • 8
    You should probably use [80 or even 72 columns](http://en.wikipedia.org/wiki/Punched_card#IBM_80_column_punched_card_format) instead – Tobias Kienzler Apr 01 '11 at 11:28
16

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.

Antti Laine
  • 139
  • 10
  • 3
    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 Burr Apr 01 '11 at 20:44
15

Your font sucks, how should a parser ever be able to read that? Take a calligraphy course.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
13

Your compilers are expecting ASCII, but that program is obviously written using EBCDIC.

oosterwal
  • 1,479
  • 8
  • 16
8

You're trying to compile an image.

Type out what you've hand written into a document called main.cpp, run that file through your compiler, then run the output file.

William
  • 19
  • 1
  • 23
    Check the date on your PC. – James P. Apr 01 '11 at 12:18
  • 14
    Haha, but I finally found an easy one I could answer! – Cody Gray - on strike Apr 01 '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 Man Apr 01 '11 at 15:04
7

You need to specify the precision of your output preceded by a colon immediately before the final closing brace. Since the output is not numeric, the precision is zero, so you need this-

:0}

MikeJ-UK
  • 620
  • 3
  • 10
5

add :

using namespace std;

right after include :P:D

Spyros
  • 46,820
  • 25
  • 86
  • 129
5

Seems that your compiler doesn't support files in such hmm... encoding. Try to convert it to ASCII.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
5

The problem lies with the syntax definition, try using ruler and compasses for a more classical description!

Cheers,

quarkonium
  • 128
  • 6
5

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.

kriss
  • 23,497
  • 17
  • 97
  • 116
5

You could try different colors for brackets, maybe some green or red would help ? I think your compiler can't rcognize black ink :P

lothar
  • 1
  • 1
5

Am I the only one who can't recognize the character between 'return' and the semicolon? That could be it!

techolic
  • 358
  • 3
  • 14
  • 4
    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 Ulhaq Apr 09 '11 at 05:03
4

Run the compiler through OCR. It might solve the compatibility issue.

James P.
  • 19,313
  • 27
  • 97
  • 155