Student needs a pair of eyes... Simple? problem

S

sandy

I think I just need a pair of eyes here... I can't see what I am doing
wrong.

I am creating a new Class for an assignment, Class File.

I have a header and a cpp file. When I try to write the constructor in
the cpp file it tells me:

File.cpp new types may not be defined in a return type
File.cpp return type specification for constructor invalid

I don't see a return type for/from my constructor so I figure it must
be confused and I have done something else wrong. Below are the header
and .cpp.

Thanks.

< HEADER CODE>
#ifndef File_h
#define File_h

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class File
{
private:
string Name, Extension;

public:

/*
Constructor

Inputs: FullName
Outputs: None

Notes: Takes in the full name (name + extension) splits it.
*/
File(string FullName);

/*
GetName

Inputs: None
Outputs Name (minus extension)
*/
string GetName();

/*
GetExtension

Inputs: None
Outputs: File Extension
*/
string GetExtension();

/*
Print

Inputs: None
Outputs: prints to screen full file name (name and extension)
*/
void Print();

/*
operator ==

Inputs: File F
Outputs: bool

Notes: If the file.Name and file.Extension are equal the files
are equal if either is not equal, the files are not equal.
*/
bool operator==(const File & F);

/*
operator <

Inputs: File F
Outputs: bool

Notes: If the full file name is < the 'other' file name True.
*/
bool operator<(const File & F);

/*
operator >

Inputs: File F
Outputs: bool
*/
bool operator>(const File & F);
}

#endif

</HEADER CODE>

<CPP CODE>
#include "File.h"
#include <iostream>
#include <cstdlib>
#include <string>

//using namespace std;

/*
Constructor
*/
File::File(string FullName)
{
int charLoc; // character location
}

</CPP CODE>
 
D

Daniel T.

I think I just need a pair of eyes here... I can't see what I am doing
wrong.

I am creating a new Class for an assignment, Class File.

I have a header and a cpp file. When I try to write the constructor in
the cpp file it tells me:

File.cpp new types may not be defined in a return type
File.cpp return type specification for constructor invalid

I don't see a return type for/from my constructor so I figure it must
be confused and I have done something else wrong. Below are the header
and .cpp.

Thanks.

< HEADER CODE>
#ifndef File_h
#define File_h

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

Don't put using declarations or definitions at global or namespace scope
in header files.
class File
{
private:
string Name, Extension;

public:

/*
Constructor

Inputs: FullName
Outputs: None

Notes: Takes in the full name (name + extension) splits it.
*/
File(string FullName);

/*
GetName

Inputs: None
Outputs Name (minus extension)
*/
string GetName();

/*
GetExtension

Inputs: None
Outputs: File Extension
*/
string GetExtension();

/*
Print

Inputs: None
Outputs: prints to screen full file name (name and extension)
*/
void Print();

/*
operator ==

Inputs: File F
Outputs: bool

Notes: If the file.Name and file.Extension are equal the files
are equal if either is not equal, the files are not equal.
*/
bool operator==(const File & F);

/*
operator <

Inputs: File F
Outputs: bool

Notes: If the full file name is < the 'other' file name True.
*/
bool operator<(const File & F);

/*
operator >

Inputs: File F
Outputs: bool
*/
bool operator>(const File & F);
}

You need a semi-colon at the end of the class definition.
 
S

sandy

Daniel said:
Don't put using declarations or definitions at global or namespace scope
in header files.

Hmmm....

What is the proper way? I could do: std:: where ever needed but that's
a bit of a pain.

Is there some other place to put a 'using' or should I just leave it
out in the headers?

I assume it is okay in the .cpp files?

(It compiles there, but perhaps there is a reason I am not aware of for
not putting it there.)
You need a semi-colon at the end of the class definition.

Thanks!

I knew I needed another pair of eyes!

You have helped me before. I really appreciate it.
 
B

BobR

(e-mail address removed) wrote in message ...
Hmmm....

What is the proper way? I could do: std:: where ever needed but that's
a bit of a pain.

Is there some other place to put a 'using' or should I just leave it
out in the headers?

If you absolutely can't type 'std::' where needed, at least limit the
pollution:

// > #include <cstdlib>
// > #include <iostream>
// move those two to the .cpp file
// unless they are actually needed in the declarations.

I assume it is okay in the .cpp files?

Same as above. (your *.cpp will only be 'included' once)
(It compiles there, but perhaps there is a reason I am not aware of for
not putting it there.)

You open the namespace needlessly in all files that you include your header
in ( remember you will most likely include it in main.cpp + others). It won't
matter much in simple 'toy' programs, but could cause trouble in larger
projects. Try to build good habits early!

As a (not standard practice) example: in my TestBench program, I use:

std::istringstream cin;
std::eek:stringstream cout;
and:
std::cout

cout<<"This goes to my text window."<<std::endl;
std::cout<<"This only goes to the debugger window."<<std::endl;

If I had 'using namespace std;' ANYWHERE, I'd get some surprises (if it even
compiled)!


For a better explanation, see the FAQ:
The comp.lang.c++ FAQ is available at http://www.parashift.com/c++-faq-lite/
 
R

red floyd

Hmmm....

What is the proper way? I could do: std:: where ever needed but that's
a bit of a pain.

Is there some other place to put a 'using' or should I just leave it
out in the headers?
>
> I assume it is okay in the .cpp files?
>

The proper way is to leave it out of the headers. Feel free to use it
(properly) inside a non-header file.

The rationale is that there may be other header files included in the
translation unit, and if they follow your header file, the using
directive will be in effect for them, which can lead to errors.

You should put it in your cpp file after all includes.
 
D

Daniel T.

Hmmm....

What is the proper way? I could do: std:: where ever needed but
that's a bit of a pain.

Call it a necessary evil.
Is there some other place to put a 'using' or should I just leave it
out in the headers?

When you are defining a an inline function in the header file, you can
put a "using namespace ..." within the function body to avoid some of
the std:: references.
I assume it is okay in the .cpp files?

Yes, after all #includes. You can put it in the cpp file because its
effect is highly localized that way, a namespace collision can be easily
resolved.
Thanks!

I knew I needed another pair of eyes!

You have helped me before. I really appreciate it.

No problem.
 
D

Daniel T.

BobR said:
(e-mail address removed) wrote in message ...

If you absolutely can't type 'std::' where needed, at least limit
the pollution:

// > #include <cstdlib>
// > #include <iostream>
// move those two to the .cpp file
// unless they are actually needed in the declarations.

#include <string>
using std::string; // only expose what you need.

Even this is frowned upon (see "C++ Coding Standards" by Sutter and
Alexandrescu)
For a better explanation, see the FAQ:
The comp.lang.c++ FAQ is available at http://www.parashift.com/c++-faq-lite/

Personally, I think the FAQ is a little too restrictive regarding this
issue. YMMV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top