Help - Search - Members - Calendar
Full Version: C++ Unexpected End of File error
BleepingComputer.com > Software > Programming
   
gnometorule
Using Windows XP
Visual C++ Express 2008 (free version)

I get the following unexpected end of file message when entering code straight from the book "Exploring C++ / Ray Lischner" (email the author claims is his in the book doesn't exist, and there is no book web forum):

------ Build started: Project: ch39_1, Configuration: Debug Win32 ------
Compiling...
generate_id.cpp
c:\documents and settings\ralf becker.ralf_work\my documents\visual studio 2008\projects\c++\ch36_42\ch39_1\ch39_1\generate_id.hpp(14) : fatal error C1004: unexpected end-of-file found
main.cpp
c:\documents and settings\ralf becker.ralf_work\my documents\visual studio 2008\projects\c++\ch36_42\ch39_1\ch39_1\generate_id.hpp(14) : fatal error C1004: unexpected end-of-file found
Generating Code...
Build log was saved at "file://c:\Documents and Settings\Ralf Becker.RALF_WORK\My Documents\Visual Studio 2008\Projects\c++\ch36_42\ch39_1\ch39_1\Debug\BuildLog.htm"
ch39_1 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The code is as follows - a header file for a class; a .cpp file for the related member functions; and a main.cpp file (very short as just learning about concepts):

generate_id.hpp

#ifndef GENERATE_ID_HPP_
#define GENERATE_ID_HPP_

class generate_id
{
public:
generate_id() : counter_(0) {}
long next();
private:
short counter_;
static short prefix_;
static long int const max_counter_ = 32767;
};
#endif

generate_id.cpp

#include "generate_id.hpp"

short generate_id::prefix_(1);
long const generate_id::max_counter_;

long generate_id::next()
{
if (counter_ == max_counter_)
counter_ = 0;
else
++counter_;
return prefix_ * (max_counter_ + 1) + counter_;
}

main.cpp

#include <iostream>
#include <ostream>

#include "generate_id.hpp"

int main()
{
generate_id gen;
for (int i = 0; i!= 10; ++i)
std::cout << gen.next() << '\n';
}


I realize that typical solutions to this problem include:

(a) Check all braces etc. close properly, and
(\b) check if any precompiled headers are used (they are not - option is disabled).

I am obviously learning C++, so maybe this is just trival for you guys. What throws me off is that the program compiles and runs 100% fine if the pre-compiler commands #ifndef/#define/#endif are removed; output produced then is as expected:

32769
.
.
.
32778

So the error only starts when adding the precompiler #conditionals. However, the IDE recognizes the conditional proper, as witnessed by...

- producing a 'missing #endif' error if i remove #endif (just to check it), and
- by indicating that #endif corresponds to #ifndef in the usual IDE way (brief blinking of #ifndef after entering #endif)

Sorry for being verbose but meant to provide info as precisely as I could. Any help would be greatly appreciated. Throws me off quite a bit if i got trouble using such precompiler commands.

Best, OGtR.
gnometorule
Issue resolved:

There needs to be a cr or similar after the #endif command. Once that is added, the program compiles fine. My apologies - might seem as if i didn't think about this before posting, which isn't true at all. Was stuck for hours but right after posting noticed. Best, O.
Billy O'Neal
Hello smile.gif

Assuming from the error code you're using a Microsoft compiler, see the following:
http://msdn.microsoft.com/en-us/library/4e...28VS.71%29.aspx

Is there a newline after the #endif in generate_id.hpp? Some compilers will choke if there's not one there.

EDIT: Looks like you beat me smile.gif

Billy3
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.