Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Unexpected End of File

Hey, I'm working on an interpreter and I keep getting errors saying:
C1004 Unexpected end of file.

Does anyone have any idea what this means? I've read various things saying that its looking for close parenthesis or something like that, but I've looked over it several times and haven't found anything. Any help would be greatly appreciated. Thanks a lot.
[388 byte] By [evlich] at [2007-11-11 7:19:26]
# 1 Re: Unexpected End of File
I note that ashvisitor and ash include each other -- this could be a problem.
jonnin at 2007-11-11 21:02:24 >
# 2 Re: Unexpected End of File
Shouldn't the #ifndef's handle this?
evlich at 2007-11-11 21:03:30 >
# 3 Re: Unexpected End of File
you're using the ideitifier AST both a class name and a namespace. This is certainly not going to work, althopugh there are probably other problems in the code.
Danny at 2007-11-11 21:04:28 >
# 4 Re: Unexpected End of File
Shouldn't the #ifndef's handle this?
#ifndef will stop a header read knowledge from other.
its only for avoiding redeclarations.
AST class dont need a complete ASTVisitor declaration.
( pointer or reference with forward declaration)
if AST realy needed ASTVisitor header ,#ifndef wouldnt help ( depending #include order. )
same for other header files in AST.h ( if they include ASTVisitor.h ) becos AST visitor seems "known" but has not been red yet)
why you add a "header" end of a file
insteed of adding them to other cpp or header files when required ?
mr1yh1 at 2007-11-11 21:05:29 >
# 5 Re: Unexpected End of File
I cant tell you how it all works, but I can tell you that include guards are not always going to work here. We did this accidentally last project, and as was mentioned, a forward decl fixed our problems and the circular includes were avoided. Our stuff had include guards but would not compile, I forget what errors it 'created' to explain its problems.
jonnin at 2007-11-11 21:06:28 >
# 6 Re: Unexpected End of File
circular references of classes are solved by using a forward declaration of one class in the other. This is a bit restricting because you can't call a member function of a forward declared class but you can use it as an argument (passed by reference) in the other class, which is what you really need. Make sure that each class has its own .cxx file which includes *only* its matching .h file and perhaps the headers that contain constants and other shared declarations. Don't #include a.h in b.cpp if a defined a.cpp.
Danny at 2007-11-11 21:07:32 >
# 7 Re: Unexpected End of File
Eureka! your project probably uses the infamous "precompiled headers" option. Disable it, and remove the "stdafx.h" header from it.
Danny at 2007-11-11 21:08:32 >
# 8 Re: Unexpected End of File
My implementation of BoolConstant requires knowledge of methods in ASTVisitor, so if I remove my include of ASTVisitor.h from AST.h and put it in BooleanConstant.cxx, then I guess I should be ok right? I also removed the old "using namespace AST" which I guess I had missed.

I checked the thing with the pre-compiled headers and I have "Create/Use Precompiled Header" on the option saying "Not Using Precompiled Headers". Also, I'm never including stdafx.h anywhere, is it automatically included some way in VS2003?
Unfortunately, even after these modifications, I'm still getting the error. Any other ideas? Thanks.
evlich at 2007-11-11 21:09:35 >
# 9 Re: Unexpected End of File
Check for unblanaced #ifndef and #endif pais in all your header files. Also, try to see what happens when you remove these directives althogether. Finally, try to collapse the two class headers into one header file. Decalre both classes in it and see if this solves the problem. Later you can split it again.
Danny at 2007-11-11 21:10:37 >
# 10 Re: Unexpected End of File
still some confusing dependicies forexample:
booleanConstant: public AST
booleanConstant designed after AST designed.
but AST.h includes( needs to know ) booleanConstant.h
...
i dont know if i miss something :
-AST needs a forward Declare to ASTVisitor , don't need ASTVisitor.h or booleanConstant.h
-ASTVisitor.h needs AST.h
-booleanConstant.h needs only ASTVisitor.h

Danny's advices about #include logic were perfect.
Don't #include a.h in b.cpp if a defined a.cpp.
mr1yh1 at 2007-11-11 21:11:39 >
# 11 Re: Unexpected End of File
Ok, I tried combining all the files into a single .h file and it worked. Then I split them apart without the #ifndef ... #endif stuff and it worked, but as soon as I put the #ifndef stuff in, it fails. I put in #error directives in else clauses to check to see if something was being included twice, but none of them are. My files are:
AST.h
#ifndef __AST_H__
#define __AST_H__

class ASTVisitor;

class AST {
public:
virtual void process(ASTVisitor &visitor) = 0;
};
#else
#error FAIL

#endif
ASTVisitor.h
#ifndef __AST_VISITOR_H__
#define __AST_VISITOR_H__
class BooleanConstant;

class ASTVisitor {
protected:
ASTVisitor();

public:
/** Constant Constructions **/
virtual void forBooleanConstant(BooleanConstant *boolConst) = 0;
};

#else
#error FAIL
#endif
BooleanConstant.h (it turns out that I have to include AST.h in this file otherwise the compiler complains that it doesn't know about the base class AST even when I just put a forward declaration)
#ifndef __BOOLEAN_CONSTANT_H__
#define __BOOLEAN_CONSTANT_H__

#include "AST.h"

class BooleanConstant : public AST {
private:
bool _value;
static BooleanConstant _TRUE;
static BooleanConstant _FALSE;

public:
bool getValue();
virtual void process(ASTVisitor &visitor);

protected:
BooleanConstant(bool value);
virtual ~BooleanConstant();
public:
static const BooleanConstant* forValue(bool b);
};

#else
#error FAIL
#endif
And BooleanConstant.cxx
#include "BooleanConstant.h"
#include "ASTVisitor.h"

BooleanConstant::BooleanConstant(bool b) {
_value = b;
}

BooleanConstant::~BooleanConstant() {
}

void BooleanConstant::process(ASTVisitor &visitor) {
visitor.forBooleanConstant(this);
}

const BooleanConstant* BooleanConstant::forValue(bool b) {
if( b ) {
return &BooleanConstant::_TRUE;
} else {
return &BooleanConstant::_FALSE;
}
}

Any ideas as to why this might happen? Thanks a lot for all your help.
evlich at 2007-11-11 21:12:38 >
# 12 Re: Unexpected End of File
Two more options to check are order dependecies, i.e, which header should be #included before which. Don't #include a header inside another header, this could lead to a total mess. Remember: a header doesn't have to compile by itself; it is always part of a larget translation unit.
Danny at 2007-11-11 21:13:42 >
# 13 Re: Unexpected End of File
///AST.h
#ifndef __AST_H__
#define __AST_H__

class ASTVisitor;

class AST {
...

///ASTVisitor.h
#ifndef __AST_VISITOR_H__
#define __AST_VISITOR_H__

#include "AST.h"
class BooleanConstant;

class ASTVisitor {
...

///booleanConstant.h
#ifndef __BOOLEAN_CONSTANT_H__
#define __BOOLEAN_CONSTANT_H__

#include "ASTVisitor.h"

class BooleanConstant : public AST {
...

///BooleanConstant.cpp
#include "BooleanConstant.h"

BooleanConstant::BooleanConstant(bool b) {
_value = b;
}
...

gcc did compile this.
only 2 linking problems undefined reference to `BooleanConstant::_TRUE'
...
mr1yh1 at 2007-11-11 21:14:37 >
# 14 Re: Unexpected End of File
So, I guess that means this is a problem with VS2003. Has anyone run into something like this before? My guess is that I probably have a compiler option set wrong, but I have no idea where to look.
evlich at 2007-11-11 21:15:39 >
# 15 Re: Unexpected End of File
Using C++ BuilderX, I didn't encounter this error but I ecnountered many other compilation errros which pertain to syntax errors. First, in one of the declarations of BooleanConstant::forValue you have a syntax error. You declare it:
BooleanConstant::BooleanConstant *forValue()

instead of
BooleanConstant * BooleanConstant::forValue();

This isn't all: I don't see why you need this class in the first place. Worst yet, you're using the symbols TRUE and FALSE as variables names which is a very bad idea because I'm almost certain that every compiler these days still retains these names as macros from the pre-bool era, which might explain why the preprocessor chokes. Finally, I don't see why a class called BooleanConstants should have a base class. Notice also:

public:
static bool* forValue(bool b);
};

you declare here a static member function that returns a non static member called _b. This is illegal.

In short, what I suugest is that you simplify this project, remove all instances of "over-engineering" (such as having a class whose sole purpose is to return a bool variable, using static data members as constants instead of using the keywords true and false etc.) and then compile this project. VS 2003 isn't a perfect compiler but I'm sure it can hjandle three header files.
The fact that GCC compiled this code doesn't promise much. You got linkage errors which suggest that something isn't defined in the project, hence, you still have to add definitions of the static data members (you only declare them).
Danny at 2007-11-11 21:16:47 >
# 16 Re: Unexpected End of File
Danny, i used last code evlich send (12. message ) with modifications (#include parts changed ).
both first and second without modifications had troubles in gcc too.
mr1yh1 at 2007-11-11 21:17:47 >
# 17 Re: Unexpected End of File
OK, using these modified source files (precisely) with C++ BuilderX, everything compiles and links just fine. Evlich, try to use these files and make sure that your compiler doesn't sneak in its usual follies: precopiled headers and "stdafx.h". Note also that I added main() to the .cpp file.
mr1yh1, does this project compile under GCC?

1:
#ifndef __AST_H__
#define __AST_H__

class ASTVisitor;

class AST {
public:
virtual void process(ASTVisitor &visitor) = 0;
};

#endif
2:
#ifndef __AST_VISITOR_H__
#define __AST_VISITOR_H__
class BooleanConstant;

class ASTVisitor {
protected:
ASTVisitor();

public:
/** Constant Constructions **/
virtual void forBooleanConstant(BooleanConstant *boolConst) = 0;
};

#endif

3:

#ifndef __BOOLEAN_CONSTANT_H__
#define __BOOLEAN_CONSTANT_H__

class BooleanConstant : public AST {
private:
bool _value;
static BooleanConstant _TRUE;
static BooleanConstant _FALSE;

public:
bool getValue();
virtual void process(ASTVisitor &visitor);

protected:
BooleanConstant(bool value);
virtual ~BooleanConstant();
public:
static const BooleanConstant* forValue(bool b);
};

#endif

4:

#ifndef __BOOLEAN_CONSTANT_CXX__
#define __BOOLEAN_CONSTANT_CXX__

#include "AST.h"
#include "ASTVisitor.h"
#include "BooleanConstant.h"

BooleanConstant::BooleanConstant(bool b) {
_value = b;
}

BooleanConstant::~BooleanConstant() {
}

void BooleanConstant::process(ASTVisitor &visitor) {
visitor.forBooleanConstant(this);
}

const BooleanConstant* BooleanConstant::forValue(bool b) {
if( b ) {
return &BooleanConstant::_TRUE;
} else {
return &BooleanConstant::_FALSE;
}
}

int main()
{}
#endif
Danny at 2007-11-11 21:18:44 >
# 18 Re: Unexpected End of File
...
mr1yh1 at 2007-11-11 21:19:43 >
# 19 Re: Unexpected End of File
i edited post a lot of times becos of wrong copy paste. :D

yes it compilied.
same 2 link errors:
undefined reference to `BooleanConstant::_TRUE'
..
mr1yh1 at 2007-11-11 21:20:47 >
# 20 Re: Unexpected End of File
Danny ,did you try it with a seperate main.cpp .
with
# include "BooleanConstant.h"

there are problems in gcc :
BooleanConstant.h:8: error: expected class-name before '{' token
...
mr1yh1 at 2007-11-11 21:21:50 >
# 21 Re: Unexpected End of File
No I used it with once .cpp file that has main() in it. The linkage errors are not a problem at the moment because they are quite expected: the program doesn't define the static data members TRUE and FALSE, it only delcares them which is of course an error, but the original problem was getting the sources to compile.
Danny at 2007-11-11 21:22:45 >
# 22 Re: Unexpected End of File
I cleaned up all the linker errors and such, I still have no idea as to why VC++ is ignoring some of my includes (I told it to generate an include file, and it said that the only thing that I was including was AST.h, the first one, even though I had more includes after that). My guess is that this is a configuration problem with my project, but I don't really know.
evlich at 2007-11-11 21:23:50 >
# 23 Re: Unexpected End of File
You have to #include .h files. Simply creating them does nothing. Adding them to the project also does nothing, or at most it checks the syntax.
jonnin at 2007-11-11 21:24:45 >
# 24 Re: Unexpected End of File
Also, you want to clean the current project settings and any remnants of incremental builds. Simply use the Rebuild All option.
Danny at 2007-11-11 21:25:51 >
# 25 Re: Unexpected End of File
place the curson after the last character you typed in the header file and hit enter to insert a newline character
synix at 2007-11-11 21:26:56 >
# 26 Re: Unexpected End of File
The newlines did it. Thanks a lot everyone.
evlich at 2007-11-11 21:27:56 >
# 27 Re: Unexpected End of File
Then your problem is you have a setup that does partial compiles (it thinks it knows what has changed and only re-compiles those files). Just do a "rebuild all" from your IDE, extra end of lines works(usually) but you might miss a file and it takes way too long.
jonnin at 2007-11-11 21:28:56 >