f



Suddenly Can't Use "ifstream"s and "ofstream"s

I had a working piece of code that successfully read from files with
the "ifstream" type and wrote to files with the "ofstream" type.  This
code is made up of a number of different files.  In the course of the
job I needed for two of these files to have access to one piece of
information, so I created a class that I called "CSuffix" and stored
the common information there.  So I created a "Suffix.h" file that
looks like:

#pragma once

class CSuffix
{
  public:

    static void initialize ();

    static void increment ();

    static char* latest ();
}

That's it in its entirety.  Now I put a line that says, '#include
"Suffix.h"' among my includes, right before the line that says,
'#include <iostream>'.  And all of a sudden the compiler doesn't
recognize either my "ifstream"s or my "ofstream"s.  All I have to do
is comment out the line that says, '#include "Suffix.h"' and then all
my "ifstream"s and "ofstream"s start working again (although obviously
the compiler then complains about my calls to my three "CSuffix"
methods).  So it looks to me like I must be doing something wrong in
my "Suffix.h" file.  Can anyone see what the problem is?

Kevin S
0
KevinSimonson
11/3/2010 11:45:04 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

7 Replies
1001 Views

Similar Articles

[PageSpeed] 56

On 03/11/2010 23:45, KevinSimonson wrote:
> I had a working piece of code that successfully read from files with
> the "ifstream" type and wrote to files with the "ofstream" type.  This
> code is made up of a number of different files.  In the course of the
> job I needed for two of these files to have access to one piece of
> information, so I created a class that I called "CSuffix" and stored
> the common information there.  So I created a "Suffix.h" file that
> looks like:
>
> #pragma once
>
> class CSuffix
> {
>    public:
>
>      static void initialize ();
>
>      static void increment ();
>
>      static char* latest ();
> }
>
> That's it in its entirety.  Now I put a line that says, '#include
> "Suffix.h"' among my includes, right before the line that says,
> '#include<iostream>'.  And all of a sudden the compiler doesn't
> recognize either my "ifstream"s or my "ofstream"s.  All I have to do
> is comment out the line that says, '#include "Suffix.h"' and then all
> my "ifstream"s and "ofstream"s start working again (although obviously
> the compiler then complains about my calls to my three "CSuffix"
> methods).  So it looks to me like I must be doing something wrong in
> my "Suffix.h" file.  Can anyone see what the problem is?
>

Missing semicolon?

/Leigh
0
Leigh
11/4/2010 12:10:34 AM
On Nov 3, 6:10=A0pm, Leigh Johnston <le...@i42.co.uk> wrote:
>
> Missing semicolon?
>
> /Leigh

Thank you!  Thank you!  That solved the problem.  Makes me wonder
though; why does the code _need_ a semicolon at the end of a header
file?  I don't put a semicolon at the end of a class implementation,
and that always compiles just fine.

Kevin S
0
KevinSimonson
11/4/2010 1:11:32 AM
KevinSimonson wrote, On 4.11.2010 2:11:
> On Nov 3, 6:10 pm, Leigh Johnston <le...@i42.co.uk> wrote:
>>
>> Missing semicolon?
>>
>> /Leigh
> 
> Thank you!  Thank you!  That solved the problem.  Makes me wonder
> though; why does the code _need_ a semicolon at the end of a header
> file?  I don't put a semicolon at the end of a class implementation,
> and that always compiles just fine.
It never compiles fine. You must be seeing things or using something else
than C++ compiler to compile it.

-- 
VH
0
Vaclav
11/4/2010 5:21:43 AM
On 2010-11-03 21:11:32 -0400, KevinSimonson said:

> On Nov 3, 6:10�pm, Leigh Johnston <le...@i42.co.uk> wrote:
>> 
>> Missing semicolon?
>> 
>> /Leigh
> 
> Thank you!  Thank you!  That solved the problem.  Makes me wonder
> though; why does the code _need_ a semicolon at the end of a header
> file?  I don't put a semicolon at the end of a class implementation,
> and that always compiles just fine.
> 

A semicolon is required at the end of a class definition.

class foo
{
};	// correct: semicolon required

class bar
{
}	// error: missing semicolon

This has nothing to do with headers, except that when you leave out the 
semicolon for a definition in a header file you'll get 
mysterious-looking errors in source files that #include that header. 
That's because the semicolon isn't actually missed until some other 
code comes along or the compiler reaches the end of the translation 
unit.

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

0
Pete
11/4/2010 12:41:58 PM
In comp.lang.c++ Pete Becker <pete@versatilecoding.com> wrote:
> A semicolon is required at the end of a class definition.

  And the reason for that is because this is valid:

class A
{
    /* something here */
} instance;

  The semicolon is needed to distinguish between a normal class definition
and that.

  Many C++ programmers don't even know this, and don't really even
understand what that means, and it's admittedly something you
extremely seldom (if ever) see in C++ programs, and it exists almost
solely as baggage from C (where using it with structs is more common).
0
Juha
11/4/2010 1:01:08 PM
On Nov 4, 1:01 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> In comp.lang.c++ Pete Becker <p...@versatilecoding.com> wrote:

> > A semicolon is required at the end of a class definition.

> And the reason for that is because this is valid:

> class A
> {
>     /* something here */
> } instance;

> The semicolon is needed to distinguish between a normal class
> definition and that.

> Many C++ programmers don't even know this, and don't really
> even understand what that means, and it's admittedly something
> you extremely seldom (if ever) see in C++ programs, and it
> exists almost solely as baggage from C (where using it with
> structs is more common).

It crops up from time to time in my code, when using local
classes.  And I've seen it occasionally in other people's code
as well.  Globally, I'd say it's more common in C++ (where most
cases involve a derived class) than in C.

--
James Kanze
0
James
11/4/2010 2:07:34 PM
On 2010-11-04 09:01:08 -0400, Juha Nieminen said:

> In comp.lang.c++ Pete Becker <pete@versatilecoding.com> wrote:
>> A semicolon is required at the end of a class definition.
> 
>   And the reason for that is because this is valid:
> 
> class A
> {
>     /* something here */
> } instance;
> 
>   The semicolon is needed to distinguish between a normal class definition
> and that.

There's nothing abnormal about combining a type definition with 
creating an object:

enum my_enum
{
first_value, second_value, third_value
} my_enumerated_value;

> 
>   Many C++ programmers don't even know this, and don't really even
> understand what that means, and it's admittedly something you
> extremely seldom (if ever) see in C++ programs, and it exists almost
> solely as baggage from C (where using it with structs is more common).

It's quite useful in many situations. You may not have encountered 
them, but that doesn't make it baggage.

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

0
Pete
11/4/2010 2:31:21 PM
Reply: