Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ Unexpected End of File error


  • Please log in to reply
2 replies to this topic

#1 gnometorule

gnometorule

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:12:38 AM

Posted 13 June 2009 - 01:31 PM

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
(\:thumbsup: 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.

BC AdBot (Login to Remove)

 


#2 gnometorule

gnometorule
  • Topic Starter

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:12:38 AM

Posted 13 June 2009 - 01:44 PM

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.

#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:09:38 PM

Posted 13 June 2009 - 01:49 PM

Hello :thumbsup:

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 :flowers:

Billy3

Edited by Billy O'Neal, 13 June 2009 - 01:51 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users